开发者

ClassNotFoundException thrown when compiling basic Hello World Program

开发者 https://www.devze.com 2023-01-09 15:35 出处:网络
I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.

I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.

I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.

I have two Jars that I need to compile a simple "hello world" program with.

Here's my "build.bat"

C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"

Then, I:

C:\jdk1.6.0_21\bin\java sample

This spits back the error:

Exception in thread "main" java.lang.NoClassDefFoundError: sample Caused by: j开发者_开发问答ava.lang.ClassNotFoundException: sample 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: at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.

Can someone please help me out? I'm pulling my hair out.


You can also specify the classpath for the interpreter to locate your class:

java -classpath "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; Sample

This would run your class from any working directory.


Run your program from the directory with the class in it:

C:\Users\FREYERA\Desktop\Test\>java sample

CLASSPATH (normally) includes the current directory.

If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:

java -cp "test1.jar;test2.jar;." sample


Afer running this command

C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"

It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class

You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.

Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号