Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compiling from the command line 5

Status
Not open for further replies.

R9772

Technical User
Aug 15, 2002
76
0
0
US
i am attempting to compile a java file from the command line and i keep recieving the following error:

Exception in thread "main" java.lang.NoClassDefFoundError

can someone help me with this?

thanks.
 
This seems to be a very common question recently.

Code:
Exception in thread "main" java.lang.NoClassDefFoundError
means that the java virtual machine cannot find the class you are attempting to call.

Can I hazard a guess this is the first time you have used the
Code:
package
statement in a java file?

The most likely cause is because you are using packages, these seems to trip most people up who have this problem.

For Example:
Code:
package com.mine;

class MyClass
{;}

the class you are calling here is NOT
Code:
MyClass
, it is
Code:
com.mine.MyClass
which needs to reside in a directory structure of com\mine\MyClass.java.
Now, to compile the above code you would use "javac com.mine.MyClass" and you would need to be in the directory that com resides in.
Example:
MyClass.java lives in
Code:
c:\Java_Apps\com\mine\MyClass.java
javac needs to be run from c:\Java_Apps, because it is from there that javac can "see" the package com.mine
The use of packages tells the JVM the name AND location of a class file

The second catch is the use of CLASSPATH. This is the list of directories that java uses to look in to search for the packages you are calling. A stock standard CLASSPATH usually contains the . directory (ie the current directory) and the location of the Java libraries
Example:
Code:
CLASSPATH=.;c:\j2sdk140_1\lib

When javac or java is executed it looks in the current directory (the .) and in the java library directory (the c:\j2sdk140_1\lib) for the packages needed. If you have other packages in other locations that are needed then you can add them to your classpath.

Be careful, you cannot have
Code:
CLASSPATH=c:\j2sdk140_1\lib;c:\Java_Apps\com\mine
as a classpath and expect the above example to work. This is because java will look in
Code:
c:\j2sdk140_1\lib
for a
Code:
com\mine\
directory and then it will look in
Code:
c:\Java_Apps\com\mine
for a
Code:
com\mine\
and obviously not find them.

There may be other reasons as well, for example the class file the JVM is unable to find may be missing totally (it's happened [ponder] )

I hope this makes it somewhat clearer for you.

-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
I'm definately a bit rusty on my java, but that may be the error that basically means it can't find a class definition in the file that has the same name as the file does. Double check capitalization and such to make sure everything matches, including the one your using on the command line.

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Hi,

I'm Bert, a java programmer. Maybe this will help your problem.

1)
your java file or classfile must be in the directory com/mycompany
If you make a directory c:\javaprograms\com\myCompany and put Helloword.class in that directory

2)try this one on the command prompt:

java -classpath c:\javaprograms com.myCompany.HelloWorld

normally it should work.

I tried it myself. This is my sequence

1)I tried this java code:

package bert;

import java.io.PrintStream;

public class HelloWorld
{

public HelloWorld()
{
}

public static void main(String args[])
{
System.out.println("Hello by BS");
}
}

I saved the HelloWorld.java file in the directory C:\temp\bert

2)
on the command prompt (on c:\):

C:\javac.exe c:\temp\bert\HelloWorld.java

3) call your class file from C:
C:\java -classpath c:\temp bert.HelloWorld

'Hello by BS' will be displayed on the command prompt.

-classpath (or -cp) adds the next parameter to your classpath see java -?
So I add c:\temp to my classpath. And java.exe starts from there searching the main class in the path bert\HelloWorld.
So from c:\temp the path bert\HelloWorld.class exists

I Hope this works.

Please report the result.

cheers
Bert
java programmer
Deinze, Belgium
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top