This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ClassPath problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ClassPath problem" Watch "ClassPath problem" New topic
Author

ClassPath problem

Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
My Program is

package Myprojects;
class Test {
public static void main(String args[]) {
Test t=new Test();
t.mulc(15,20);
t.mulc(-15,20);
t.mulc(15,-20);
}
void mulc(int a ,int b) {
//assert a>=0;
//assert b>=0;
int val=a*b;
System.out.println("Value is "+val);
//assert val>=0:"Valur cannot be negative";
}}

It is in the package called MyProjects
the package hierarchy is
C:\MyProjects
now,in XP
if i give the command
java MyProjects.Test
then the compilier is giving

C:\>java MyProjects.Test
Exception in thread "main" java.lang.NoClassDefFoundError: MyProjects/Test (wron
g name: Myprojects/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Probably,it means compiler cannot find the class file.

now,how to run the program from C:\ ?
sarada chellu
Greenhorn

Joined: Sep 25, 2007
Posts: 17
How did you complied ?
In this way c:/>javac -d . Test.java
If you simply try to complie c:/>javac Test.java
it doesn't create a folder/directory named Myprojects and gives you java.lang.NoClassDefFoundError
Please check the way you are compliling.
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
I compiled
javac MyProjects\Test.java
It compiled successfully
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
My Program is under C:\MyProjects
Directory
sarada chellu
Greenhorn

Joined: Sep 25, 2007
Posts: 17
c:/>javac -d Myprojects\. Myprojects\Test.java
c:/>java -cp Myprojects Myprojects.Test

See your java source file is in Myprojects folder and your trying to use package Myprojects in test.java so it's some thing like Myprojects\Myprojects\Test
so you try with the above to make it run.
Shelly Biswal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
hi was just wondering what does java -cp exactly do and what parameters does it take???
sarada chellu
Greenhorn

Joined: Sep 25, 2007
Posts: 17
-cp is for classpath seting.
we can search path for directories and jar/zip files
In the previous eg I am searching for a file in Myprojects directory
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Sarada,
the command
c:/>java -cp Myprojects Myprojects.Test
is not working

It gives
Exception in thread "main" java.lang.NoClassDefFoundError: MyProjects/Test (wron
g name: Myprojects/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

which probably means that compilier cannot find the class.
By the way,
in the above javac command,
why do you need to mention MyProjects twice?
also,
what is the dot in the line below?
javac -d MyProjects\. MyProjects\Test.java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassPath problem
 
Similar Threads
BeerExpert NoClassFoundDefError Issue (NOT COMPILE ERROR) - HF Servlets & JSP
Assertion
Issues with cmd java command
Reading a file using Scanner
Calling main method outside of its class