Re: [Py4j-users] How to run Stack example in the docs?
Status: Beta
Brought to you by:
barthe
From: listserve <tom...@gm...> - 2014-12-08 18:07:02
|
Yeah that seems to be the problem. The following works for me: Stack.java ------------------------------------- import java.util.LinkedList; import java.util.List; public class Stack { private List<String> internalList = new LinkedList<String>(); public void push(String element) { internalList.add(0, element); } public String pop() { return internalList.remove(0); } public List<String> getInternalList() { return internalList; } public void pushAll(List<String> elements) { for (String element : elements) { this.push(element); } } } ------------------------------------- StackEntryPoint.java ------------------------------------- import py4j.GatewayServer; public class StackEntryPoint { private Stack stack; public StackEntryPoint() { stack = new Stack(); stack.push("Initial Item"); } public Stack getStack() { return stack; } public static void main(String[] args) { GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint()); gatewayServer.start(); System.out.println("Gateway Server Started"); } } ------------------------------------- I then compiled and ran with: > export JARFILE=/usr/local/share/py4j/py4j0.8.2.1.jar > javac -cp $JARFILE *.java > java -cp .:$JARFILE StackEntryPoint Thanks a lot for the package! Now that I'm able to I hope to make use of it. :p Just a little. It might be better to use the code as I wrote it instead of the way it currently is in the docs. I've used Java in the past, but it's been a while and I could never make sense of the error codes nor could I make it work without emailing the list. Searching on the internet wasn't really that helpful either because the errors were fairly generic. I'm pretty sure other Java neophytes might have similar issues since they're probably often like me not actually good at Java, but have some java that needs to be wrapped in Python. I think making this as brain-dead easy as possible will probably help adoption. In any case, thanks so much for writing this. I'm excited to make use of it! Cheers, Thomas On 12/08/2014 12:36 PM, Barthelemy Dagenais wrote: > Hi, > > thanks for using Py4J! > > I believe the problem might be the package. If you copied/pasted the > code exactly, the Java source files need to be in the appropriate > directories (e.g., py4j/examples/StackEntryPoint). After compiling the > classes, you need to specify the fully qualified name of the main > class when invoking java: > > java -cp .:$JARFILE py4j.examples.StackEntryPoint > > I suggest though that you just remove the "package py4j.examples;" > line in both files. You should then be able to compile and run the > example code. > > Alternatively, you can use the StackEntryPoint class that is provided by py4j: > java -cp $JARFILE py4j.examples.StackEntryPoint > > HTH, > Barthelemy > > On Mon, Dec 8, 2014 at 11:39 AM, listserve <tom...@gm...> wrote: >> Hello, >> >> I'm trying to run the code in the docs from here: >> >> http://py4j.sourceforge.net/getting_started.html#writing-the-java-program >> >> I'm not that experienced with Java so I may very well be making a >> mistake unrelated to py4j, but I'm unable to run the code regardless. >> This is what I've done: I saved the Stack code in a file called >> Stack.java and the other code in StackEntryPoint.java. I compiled it at >> the command line with the following: >> >> JARFILE=/usr/local/share/py4j/py4j0.8.2.1.jar >> javac -cp $JARFILE *.java >> >> That gave no error. I then tried to run it with: >> >> java -cp $JARFILE:. StackEntryPoint >> >> and I get the following error: >> >> Exception in thread "main" java.lang.NoClassDefFoundError: >> StackEntryPoint (wrong name: py4j/examples/StackEntryPoint) >> at java.lang.ClassLoader.defineClass1(Native Method) >> at java.lang.ClassLoader.defineClass(ClassLoader.java:800) >> at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) >> at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) >> at java.net.URLClassLoader.access$100(URLClassLoader.java:71) >> at java.net.URLClassLoader$1.run(URLClassLoader.java:361) >> at java.net.URLClassLoader$1.run(URLClassLoader.java:355) >> at java.security.AccessController.doPrivileged(Native Method) >> at java.net.URLClassLoader.findClass(URLClassLoader.java:354) >> at java.lang.ClassLoader.loadClass(ClassLoader.java:425) >> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) >> at java.lang.ClassLoader.loadClass(ClassLoader.java:358) >> at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) >> >> I am running Linux Mint 17 (which is basically Ubuntu) and the following >> is my versions of java/javac: >> >> $ java -version >> java version "1.7.0_65" >> OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-0ubuntu0.14.04.1) >> OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode) >> >> $ javac -version >> javac 1.7.0_65 >> >> Thanks for any help. >> >> Cheers, >> Thomas >> >> ------------------------------------------------------------------------------ >> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server >> from Actuate! Instantly Supercharge Your Business Reports and Dashboards >> with Interactivity, Sharing, Native Excel Exports, App Integration & more >> Get technology previously reserved for billion-dollar corporations, FREE >> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk >> _______________________________________________ >> Py4j-users mailing list >> Py4...@li... >> https://lists.sourceforge.net/lists/listinfo/py4j-users > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users > |