Calling Java .jar Program from Perl using System() - java

The title pretty much says it all, I am trying to get a java program (.jar file) to run from a Perl program. I read another post on Stackoverflow saying this syntax is correct:
system("java filename.jar");
However this is giving me the following error. I'm not sure if the problem is that it shows the filename as "filename/jar" instead of "filename.jar"
Exception in thread "main" java.lang.NoClassDefFoundError: filename/jar
Caused by: java.lang.ClassNotFoundException: filename.jar
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

You need to call it as java -jar filename.jar.
If you leave out the -jar, then java thinks you're trying to run a class named filename.jar, which it tries to load from the filename/jar file. Since you don't have that file, java throws the error that you see.

You have to use the -jar option to launch a jar.
java [ options ] -jar file.jar [ argument ... ]
Resources :
sun.com - java

Related

"java.lang.NoClassDefFoundError" Running java using cmd but getting error

Running java using cmd following commands but getting error
java -cp %etc_folder%%service%%library%
i did set up environment variable for etc_folder, service, library
library envi: target\service_service-05.2015.jar
I check the folder jar file is in folder.
getting following error:
Exception in thread "main" java.lang.NoClassDefFoundError: target\service_service-05/2015/jar;
caused by: java.lang.ClassNotFoundException: target\service_service-05.2015.jar;
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)
I think it's replacing "." to "/"
not sure why, can anyone please help with this.
Thank in advanced!!
You should execute Java as following pattern:
java -cp $(echo /opt/path/lib/*.jar| tr ' ' ':') com.xxx.xxx.MainClass
All your jar putting into /opt/path/lib/,
com.xxx.xxx.MainClass is which class you have main() function.

Not Able to Compile Java Code via Command-line

I wrote some Java code that takes several arguments using Eclipse. I was able to compile and execute the code just fine by entering the arguments in the "Run configurations."
However, I need to be able to run my code via command-line. This is what I tried:
javac -g ./MyCode.java
java MyCode ./fileOne.txt ./fileTwo.txt ./fileThree.txt
And the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/MyCode
Caused by: java.lang.ClassNotFoundException: mypackage.MyCode
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)
I don't understand what is going on. My code does not have any bugs when I run through Eclipse. Help?
You are not setting up the classpath properly.
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

java.lang.NoClassDefFoundError when using weka.jar file in my own java file

I'm having problems executing my java program. The code is placed in the file RunMining.java and contains multiple import weka.[..] lines. The weka library (weka.jar) is placed in the same folder.
I compile it:
$ javac -cp weka.jar RunMining.java
This creates three files: RunMining.class, RunMining$1.class, RunMining$classifierType.class
When i run it: java -cp weka.jar RunMining i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: RunMining
Caused by: java.lang.ClassNotFoundException: RunMining
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)
My RunMining.java file only contains one class called RunMining, which contains the main method. I would prefer not to add the weka.jar file to my CLASSPATH since this file will be compiled using a makefile and executed using a script, on different machines.
Any suggestions, to how i can run the compiled files?
You need to add the classpath of your compiled files as follows:
java -cp weka.jar;. RunMining
This assumes your files are compiled in the root directory which your javac command suggests.

Javac compilation and execution of 2 java files

The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. I wrote that on windows on eclipse but want to compile it on Linux javac compiler.
javac filter.java ClasifiedWord.java
runs without mistakes, but when I try to run the program:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
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)
Could not find the main class: filter. Program will exit.
How can I run the program ? It seems that the compilation is ok. I have doubts that maybe I have made a mistake about the file paths in the program.... but I think that this is not the case?
Thanks for your answers!
Type the following command.
java -classpath . filter
If you want to type just java filter, please following these guides.
Linux http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/
Windows http://ac-support.europe.umuc.edu/~arnoldyl/NetBeansTutorials/Setting-Classpath.html
P.S. The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument.
Roland is right in the comment, you should be able to run it with java -classpath <folderContainingDotClassFiles> filter. Also, you should change filter.java to Filter.java as the standard Java convention for class names (including the files containing them) is to capitalize the first letter.
This looks like you either have a wrong CLASSPATH setting (usually you don't need this at all, but if you have it, it should include . in the list, for the current directory), or you are using packages (wrongly), or both. For the classpath issue, see the other answers.
If you are using packages, i.e. your files contain a package ...; line at the beginning, you should put your source files in a directory structure according to the package structure. For example, if you have package example;, you should have an example directory in your current directory, and in this the two source files.
Then call compiler and interpreter like this:
javac example/filter.java example/ClasifiedWord.java
java example.filter
(This assumes that filter is the main class, otherwise replace its name.)

Java: Exception in thread "main" java.lang.NoClassDefFoundError

To compile my java app, I use from terminal:
javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer.java
To run the app I use:
java -classpath commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer
However I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: commons-digester-2/1/commons-digester-2/1/jar:lucene-core-3/0/3/jar
Caused by: java.lang.ClassNotFoundException: commons-digester-2.1.commons-digester-2.1.jar:lucene-core-3.0.3.jar
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I guess I'm defining correctly the classpath.. or what ?
Try -cp instead of classpath. Also add the current directory to the classpath.
$ java -cp .:commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar CollectionIndexer
or
$ export CLASSPATH=.:commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar
$ java CollectionIndexer
If on Windows, use backslashes and a semi-colon separator:
java -cp .;commons-digester-2.1\commons-digester-2.1.jar;lucene-core-3.0.3.jar CollectionIndexer
I would suggest you to set the classpath first with
SET CLASSPATH command and then execute your program as
java CollectionIndexer.

Categories