java.lang.ClassNotFoundException while executing a class file from inside a jar - java

I created a jar file in windows & copied it to linux and tried to execute with the following command:
java -jar Test.jar com.dcat2.messaging.datatransfer.Test
I am getting an exception as follows:
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.
I am executing this command in the same location where i have the jar file.
Here is my java code:
package com.dcat2.messaging.datatransfer;
public class Test {
public static void main(String[] args) {
System.out.println("Test App : " + args[0]);
}
}
The jar -tf command's output is as follows:
jar -tf Test.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/dcat2/
com/dcat2/messaging/
com/dcat2/messaging/datatransfer/
com/dcat2/messaging/datatransfer/Test.class
Can anyone help please?

java -cp Test.jar com.dcat2.messaging.datatransfer.Test
The -jar option launches the class that is indicated as the main class in the manifest of the jar file (and which must be the Test class, in the default package, in your case).
The -cp option sets the classpath to use for the app which, in this case, only contains your test jar.

Related

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.

running a java project from command line using classpath issue?

I am trying to run a java project from the command line in linux, my project uses two external jar files. The command that i am givin is
java -classpath -jar bin:common-cli-1.2.jar:BuildFrameworkLibrary.jar com.kpit.goa.common.tools.kivibuild.KIVIBuild
where KIVIBuild is the class that contains the main function. But the error that am getting is:
baibhav#baibhav:~/git/KiviBuild/Infra/RepoManagement/BuildManagement/KIVIBuild$ java -classpath bin:common-cli-1.2.jar:BuildFrameworkLibrary.jar com.kpit.goa.common.tools.kivibuild.KIVIBuild
Gives
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
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: com.kpit.goa.common.tools.kivibuild.KIVIBuild. Program will exit.
You need a path separator e.g.
bin/common-cli-1.2.jar:BuildFrameworkLibrary.jar
The colon separates paths to individual jar files. e.g. in the above you're referencing the two files
bin/common-cli-1.2.jar
BuildFrameworkLibrary.jar
and also reference the directory containing your compiled classes e.g. if they're in (say) target/classes then use:
target/classes:bin/common-cli-1.2.jar:BuildFrameworkLibrary.jar
(relative to your current working directory)
Use following command
javac -classpath bin/common-cli-1.2.jar:bin/BuildFrameworkLibrary.jar KIVIBuild.java -d .
Do not forget to add dot at the end of command
check is /com/kpit/goa/common/tools/kivibuild/KIVIBuild.class exist or not
if yes run the following command
java -classpath bin/common-cli-1.2.jar:bin/BuildFrameworkLibrary.jar com/kpit/goa/common/tools/kivibuild/KIVIBuild
http://www.linuxheadquarters.com/howto/classpath.shtml
Rather than setting class path every time set it onces in existing classpath variable and just run the java command

ClassNotFound Exception while trying to run jar in unix server

I created a executable jar of my Java project I tested the jar and it is working as expected in windows but when i try run the same jar in Unix..
java jar my.jar
..it is throwing me the error below:
Exception in thread "main" java.lang.NoClassDefFoundError: jar
Caused by: java.lang.ClassNotFoundException: jar
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)
How to overcome the above error?
It should be
java -jar my.jar
not the
java jar my.jar
See
java - the Java application launcher
In a runnable jar file , there will be a MANIFEST.MF inside the META-INF folder. Inside MANIFEST.MF there should be an entry Main-Class: com.myapp.MyMainClass . This will be created automatically if you using the jar command to make a runnable jar file.To be sure,unjar the jar and see if this entry is present.

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

NoClassDefFoundError thrown when specifying same jar file at compile and run time

Hi I'm trying to use an external Java package from my own code and keep getting NoClassDefFoundError even though I am using the same class path that I compiled with.
For your amusement I have included a bare bones reproduction of what will undoubtedly be a dumb mistake on my part (I've been at this for about 6 hours so far)
/* WTF/WTF.java */
import foo.Bar;
class WTF
{
public static void main(String[] args)
{
Bar dontCare = new Bar();
}
}
/* WTF/foo/Bar.java */
package foo;
class Bar
{
public Bar() {}
}
Now from the WTF directory I run the following:
javac foo/Bar.java [ok]
javac WTF.java [ok]
java WTF [ok]
jar cf foo.jar foo [ok]
I remove the WTF/foo directory so there is only WTF/foo.jar available.
javac WTF.java -cp foo.jar [ok]
java WTF -cp foo.jar [$!##$!]
Exception in thread "main" java.lang.NoClassDefFoundError: foo/Bar
at WTF.main(WTF.java:7)
Caused by: java.lang.ClassNotFoundException: foo.Bar
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)
Any clues greatly appreciated!
The directory where is WTF.class should be in the classpath.
Also, Bar should be public.
java -cp foo.jar:. WTF
: is the path separator in Linux, if you are using Windows replace it with ;.
Wrong arguments sequence? Try:
java -cp foo.jar WTF

Categories