I was having trouble with a could not find main class error with a somewhat complicated program I was working on. To eliminate possible problems I decided to try a hello world program to see if I could get that to work. I am working on a server which I'm pretty sure is running Red Hat Enterprise 6. I followed these steps provided by Bart Kiers in answer to this question:
create a file called HelloWorld.java;
paste the code posted below
inside HelloWorld.java: compile it by executing the command: javac
HelloWorld.java in the same folder as HelloWorld.java is in;
execute the code by doing: java -cp . HelloWorld in the same folder as
HelloWorld.java is in.
I get the following error after the last step:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/
Caused by: java.lang.ClassNotFoundException: HelloWorld.
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: HelloWorld.. Program will exit.
If I type java -version, the version info displays, suggesting that my PATH variable is set correctly. Does anyone have any other suggestions for things that might be causing this error? Thanks!
Looks like you are putting an period at the end of your class name:
java -cp . HelloWorld.
Do this instead
java -cp . HelloWorld
Related
I am using the command:
java -cp my.jar myClass
but I am getting these errors.
Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.tools.ant.Task
at java.lang.ClassLoader.defineClassImpl(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:295)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:711)
at java.net.URLClassLoader.access$400(URLClassLoader.java:92)
at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:1159)
at java.security.AccessController.doPrivileged(AccessController.java:314)
at java.net.URLClassLoader.findClass(URLClassLoader.java:594)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:743)
at java.lang.ClassLoader.loadClass(ClassLoader.java:711)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
at java.lang.ClassLoader.loadClass(ClassLoader.java:690)
at java.lang.ClassLoader.defineClassImpl(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:295)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:711)
at java.net.URLClassLoader.access$400(URLClassLoader.java:92)
at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:1159)
at java.security.AccessController.doPrivileged(AccessController.java:314)
at java.net.URLClassLoader.findClass(URLClassLoader.java:594)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:743)
at java.lang.ClassLoader.loadClass(ClassLoader.java:711)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
at java.lang.ClassLoader.loadClass(ClassLoader.java:690)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:494)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Task
at java.net.URLClassLoader.findClass(URLClassLoader.java:599)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:743)
at java.lang.ClassLoader.loadClass(ClassLoader.java:711)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
at java.lang.ClassLoader.loadClass(ClassLoader.java:690)
I am a novice when it comes to all this, but am trying to avoid using ANT (another thing I am a novice at) in order to run the class we need. Any help would be greatly appreciated!
Try to add the ant.jar also to your claspath
on Windows:
java -cp .;my.jar;pathofantjar/ant.jar myClass
or on linux:
java -cp .:my.jar:pathofantjar/ant.jar myClass
In general, this error means that the class in question was present at compile time, but cannot now be found on the classpath or in the JAR. The problem is that the classpath you are using when you are running your code is different from the classpath that you used when compiling your code.
You should post your code, as the error indicates your code depends on an ant library, and you say you're trying to avoid that.
You need to set your class path to see org.apache.tools.ant.Task. Something like
java -cp my.jar; /path/to/apache/tools.jar myClass
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
I used ObjectWeb's ASMifier to get a 'HelloDump.java', and added classloader and a main method to load and run a spoofed "HelloWorld".
If I run 'HelloDump.java' in the build tool sbt, everything works fine and outputs "HelloWorld!".
But if I use raw Java, it breaks. 'HelloDump.java' seems to compile OK, but the resulting 'HelloDump.class' is clearly present, yet doesn't seem to be recognized (check the sequence below):
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ javac -cp lib/asm-all-4.1.jar HelloDump.java
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ ls
DumpLoader.java.bak HelloDump.class.bak Hello.java.bak
Hello.class.bak HelloDump$DynamicClassLoader.class lib
HelloDump.class HelloDump.java README.md
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ java -cp lib/asm/all/4.1.jar HelloDump
Exception in thread "main" java.lang.NoClassDefFoundError: HelloDump
Caused by: java.lang.ClassNotFoundException: HelloDump
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: HelloDump. Program will exit.
Explicitly adding . to the classpath doesn't help either.
So why does this work in sbt, but not in raw Java, and how can I fix it?
Thanks, any advice is appreciated,
-Julian
Add . to your CLASSPATH, so your command becomes:
java -cp lib/asm-all-4.1.jar:. HelloDump
The reason the JVM cannot find the class is that it looks only on the classpath and nowhere else.
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.
I'm trying out OpenNI for Kinect and got it to install and run sample code according to
this guide. But now, I want to modify the code and compile it and test it. However, I'm not sure how to compile and run on Linux. I found a guide here that does it for Windows, but can't seem for the life of me to find anything for Linux.
I did try adapting the Windows code for Linux and tried
javac -cp ~/kinect/OpenNI/Samples/Bin/x86-Release/org.OpenNI.jar VersionInfo.java
java -cp ~/kinect/OpenNI/Samples/Bin/x86-Release/org.OpenNI.jar VersionInfo
but it gives me
Exception in thread "main" java.lang.NoClassDefFoundError: VersionInfo
Caused by: java.lang.ClassNotFoundException: VersionInfo
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: VersionInfo. Program will exit.
Any ideas on how I can compile and run my java code onto the Kinect? If you know how to do it for the Samples in the OpenNI folder, that would be perfect.
Nevermind, figured it out. Turns out I needed to learn about make and Makefiles and how they're used to compile .java files into a .jar.