Jython How to Use Standalone JAR? - java

I am trying to use the Jython PythonInterpreter class, but am struggling a bit.
Due to physical limitations, my scenario lacks any sort of project structure. I simply have a Jython JAR file and a Java file.
I am currently compiling my Java code like so:
javac -cp "jython-standalone-2.7.0.jar" test.java
This command does not fail.
When I try to run the compiled Java, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/util/PythonInterpreter
at test.main(test.java:9)
Caused by: java.lang.ClassNotFoundException: org.python.util.PythonInterpreter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The code looks like so:
import org.python.util.PythonInterpreter;
class test {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
}
}
The code is very simple. Should I be importing the Jython classes a different way? Does my method here have any chance of working?
Thanks

How do you run the command?
You will also need to set the CLASSPATH.

Related

JUnit exception in main?

I am trying to run a simple junit test using OS X and terminal. I have placed the Junit jar file inside my java folder. I was able to compile all of the files using:
javac -cp .:"/Library/java/junit.jar" *.java
It compiles just fine, with no errors. However when I try to run the command:
java TestRunner
It gives the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
at TestRunner.main(TestRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I cannot seem to find what I am doing incorrectly. Any help solving this would be greatly appreciated.
Like #ToddSewell said, the external libraries should be in your classpath for the execution too. Try this:
java -cp .:"/Library/java/junit.jar" TestRunner

Running Java classes in Hadoop

I am trying to run a java class in Hadoop, just like in the book "Hadoop.The.Definitive.Guide", like this:
hadoop URLCat hdfs://localhost/user/hamza/sometext.txt
Where it is supposed to print the contents of sometext.txt to the terminal.
What happens is when I type that command it gives me this error:
Error: Could not find or load main class URLCat
URLCat is a Java class, but I have no idea why it's not working.
I tried converting it to a jar file using IntelliJ but then when I run it, like this:
hadoop jar Hadoop_example.jar URLCat hdfs://localhost/user/hamza/sometext.txt
Note: Hadoop_example.jar is the jar file name and the class that has main method is URLCat.
It gives me this error:
Exception in thread "main" java.lang.ClassNotFoundException: URLCat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
So how can I run Java classes in Hadoop.
I found a similar question on Stack overflow but the answers were mostly to convert it into a jar file, but that didn't work for me either.
hadoop jar Hadoop_example.jar URLCat hdfs://localhost/user/hamza/sometext.txt
This is the correct command line to start a program on hadoop. Why it does not start depends on how you made the jar.
This POST will be useful to you.

openmpi + java, could not find or load main class

I am recently trying out the java binding of the openmpi library :
I have succesfully compiled the lib with the following configurations:
$/configure --prefix "/home/yuechuan/Application/.openmpi" --enable-mpi-java --with-jdk-dir="/usr/lib/jvm/java-8-oracle/" --with-jdk-header="/usr/lib/jvm/java-8-oracle/include/"
$sudo make install
I am able to compile a simple java program with : $mpijavac src/com/cyc115/pa2/Main.java
currently $echo $CLASSPATH gives /home/yuechuan/Github/parallel_pa2/src/com/cyc115/pa2/
However I am having trouble to get the class file to run. Here are some alternatives I have tried so far:
$java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/mpi.jar -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar Main
which returns Error: Could not find or load main class Main error.
the pure and simple $java Main command returns NoClassDefFoundError
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: mpi/MPIException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: mpi.MPIException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Here's the java file if anyone needs :
package com.cyc115.pa2;
import mpi.MPI;
import mpi.MPIException;
public class Main {
public static void main(String args[]) throws MPIException
{
MPI.Init(args);
int rank = MPI.COMM_WORLD.getRank();
int size = MPI.COMM_WORLD.getSize();
System.out.print("hello world");
MPI.Finalize();
}
}
Any ideas on how I can get the .class to run?
Since the package is :
package com.cyc115.pa2;
You need to put the file Main.java in a sub-directory named com/cyc115/pa2
The java library path should be a path to the directory containing libmpi_java.so rather than the path to mpi.jar.
To compile:
javac -cp /home/yuechuan/Application/.openmpi/lib/mpi.jar com/cyc115/pa2/Main.java
To run:
java -Djava.library.path=/home/yuechuan/Application/.openmpi/lib/ -cp .:/home/yuechuan/Application/.openmpi/lib/mpi.jar com.cyc115.pa2.Main
The following is not the problem, just an FYI:
It should be --with-jdk-headers with an s rather than --with-jdk-header however you do not have to specify it at all if you are already specifying --with-jdk-dir.

How to compile and run Stanford's Karel with the command line?

This program compiles and runs swimmingly in eclipse on the same machine, but I'd really like to work from the command line and my editor of choice.
CollectNewspaperKarel.java
import stanford.karel.*;
public class CollectNewspaperKarel extends SuperKarel {
public void run() {
// You fill in this part
}
}
The karel.jar is in the same directory as the file above.
Compile
javac -cp karel.jar CollectNewspaperKarel.java
with no errors.
Run
java -cp karel.jar CollectNewspaperKarel
Exception in thread "main" java.lang.NoClassDefFoundError: CollectNewspaperKarel
Caused by: java.lang.ClassNotFoundException: CollectNewspaperKarel
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
you forgot to include your class to classpath when running the program.
Try this - if running on windows:
java -cp karel.jar;CollectNewspaperKarel.class CollectNewspaperKarel
or this if you're running on linux:
java -cp karel.jar:CollectNewspaperKarel.class CollectNewspaperKarel
One more thing however, you need to have main method in your class, otherwise it won't work :)
Update:
I've found following site: http://ycsoftware.net/setting-up-karel-the-robot-in-eclipse/
seems, you should go with the following arguments if you have the same version of karel as the author there:
on windows:
java -cp karel.jar;CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
on linux:
java -cp karel.jar:CollectNewspaperKarel.class stanford.karel.Karel code=CollectNewspaperKarel
Peter B. is right, however you cannot run a class containing a "run" method alone, you need a public static void(String[] args) method to make it runnable.
I suppose that in Eclipse some other class is used as a "main class" to run the thing.

HtmlUnit first steps

I am trying to set up my first example program with HtmlUnit. This is the code:
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebClient;
public class test {
public static void main(String[] args) throws Exception {
WebClient client = new WebClient();
HtmlPage currentPage = client.getPage("http://www.oddsportal.com/matches/soccer");
client.waitForBackgroundJavaScript(10000);
String textSource = currentPage.asXml();
System.out.println(textSource);
}
}
then i compile:
javac -cp lib/htmlunit-2.9.jar test.java
but when i try to exec test i get
java -cp lib/htmlunit-2.9.jar test
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
where is the problem? i lack some other packages?
you may have to add the current path to the classpath.
On Unix/Linux/Mac:
java -cp .:lib/htmlunit-2.9.jar test
On Windows:
java -cp .;lib/htmlunit-2.9.jar test
EDIT
There is actually more jars needed for htmlunit that just htmlunit-2.9.jar. Therefore, considering that all those required jars are located in the lib/ directory, you should actually invoke the following:
On Unix/Linux/Mac:
java -cp .:lib/* test
If you run this command from a shell, you also need to escape the wildcard (with a '\') or put the whole classpath within quotes to avoid shell expansion.
On Windows:
java -cp .;lib/* test
This works:
java -cp .:lib/* test

Categories