Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassPath problem

 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled
javac MyProjects\Test.java
It compiled successfully
 
Ayanik Sil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Program is under C:\MyProjects
Directory
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi was just wondering what does java -cp exactly do and what parameters does it take???
 
sarada chellu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic