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.
Related
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.
I am able to compile code but while running it is throughing classnotfound
C:\Users\Desktop\New>javac -classpath java-mail-1.4.4.jar SendMail.java
C:\Users\Desktop\New>java -classpath java-mail-1.4.4.jar SendMail
Exception in thread "main" java.lang.NoClassDefFoundError: SendMail
Caused by: java.lang.ClassNotFoundException: SendMail
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)
Could not find the main class: SendMail. Program will exit.
Can you suggest how to solve this?
Try using below commands
To compile java class
javac -classpath .;<jar file path>; Abc.java
To execute java class
java -classpath .;<jar file path>; Abc
Try using:
Windows
java -classpath java-mail-1.4.4.jar;. SendMail
note this ----^
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.
I am new to java..
I have an example structure as follows:
/folder/foo.java /folder/bar.java /folder/foobar.java
Now I am trying to run foobar.java but I get the following exception
Exception in thread "main" java.lang.ClassNotFoundException: /folder/foobar
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.hadoop.util.RunJar.main(RunJar.java:149)
Then I tried to configure it by doing
java -classpath . foobar
Exception in thread "main" java.lang.NoClassDefFoundError: foobar
Caused by: java.lang.ClassNotFoundException: foobar
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)
Any clues?
Thanks
The default value of a classpath is ‘.’ , i.e. the current directory. The value of the classpath environment variable overrides this value.
If the java command is issued with –cp or –classpath option, it overrides the default ‘.’ and classpath environment variable value.
Below is an example for setting a classpath during class execution
C:>java -classpath "." com.abc.example.SayHello
As opposed to compiling where you need to give exact path, to run the class file, we need to follow the package structure.
This is due to the way the Classloader tries to resolve the class location by combining its package and class name. You must be on the package root location and issue the java command specifying the package structure.
C:>java com.abc.example.SayHello
Hello!!
First, you need to compile java files:
javac /folder/*.java
Then you can run a class with main() function:
java -cp . folder.foobar
I am trying to command line compile my code and I cannot get it to run properly. My project was in eclipse and I could specify the location of a jar file to include but the command line is another issue. All .java and the .jar file are in the same director.
I can compile with
javac -classpath opencsv-2.3.jar *.java
I get the following exception if I try to run my code.
java ClassificationGenerator
Exception in thread "main" java.lang.NoClassDefFoundError: au/com/bytecode/opencsv/CSVReader
I tried running the code with
java -classpath opencsv-2.3.jar ClassificationGenerator
but then I get
Exception in thread "main" java.lang.NoClassDefFoundError: ClassificationGenerator
Caused by: java.lang.ClassNotFoundException: ClassificationGenerator
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 need to give the jar in the classpath while running, aw well:
java -classpath .;opencsv-2.3.jar ClassificationGenerator