views:

108

answers:

3

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.

+1  A: 

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

mwalker
The working directory was the C:\Users\FREYERA\Desktop\Test\> directory. Didn't seem to make a difference.
Andrew J. Freyer
Java's case sensitive. Is your class named "sample" or "Sample"?
mwalker
A: 

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.

Marimuthu Madasamy
A: 

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.

Kamal