Search code examples
javawindows-7javacjava-6

ClassNotFoundException thrown when compiling basic Hello World Program


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: java.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.


Solution

  • 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.