perfect_tranquility
asked on
Classpath Issue?
1).class1.java is in c:\a\b\folder1
2).Compiled with no issues: C:\a\b> javac -classpath c:\a\b folder1\class1.java
3).Tried to run the generated class file: C:\a\b>java -classpath c:\a\b folder1.class1
4).Error Stack: Exception in thread "main" java.lang.NoClassDefFoundE
at java.lang.ClassLoader.defi
at java.lang.ClassLoader.defi
at java.security.SecureClassL
at java.net.URLClassLoader.de
at java.net.URLClassLoader.ac
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
What mistake did I commit in classpath and/or folder structure?
Thanks
Roopesh
Code of c:\a\b\folder1\class1.java
-----------------------------------------------------
package a.b.folder1;
class class1{
public void f1(){
System.out.println("FUNCTION OF CLASS1");
}
public static void main(String[] argsss)
{
System.out.println("MAIN OF CLASS1");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Avdej:
Kudos
1 and 2 worked.
3 did not.
3a. did
Avdej, can we use -cp for javac command also? If yes, could you give explain based on above?
Thanks. You not only provided a good solution but also had time to write and explain it.
Kudos
1 and 2 worked.
3 did not.
3a. did
Avdej, can we use -cp for javac command also? If yes, could you give explain based on above?
Thanks. You not only provided a good solution but also had time to write and explain it.
ASKER
Avdej,
I have two questions here:
Question 1:
I also notice that if I do c:\somefolder and then try 3a, it fails,why?
e.g. I navigated to c:\Program Files and did the following two and got the same error
1).java -cp c:\a\b folder1.class1
2).java -cp c:\ a.b.folder1.class1
Error got:
C:\>java -cp c:\a\b folder1.class1
Exception in thread "main" java.lang.NoClassDefFoundE rror: folder1/class1 (wrong
name: a/b/folder1/class1)
at java.lang.ClassLoader.defi neClass1(N ative Method)
at java.lang.ClassLoader.defi neClass(Un known Source)
at java.security.SecureClassL oader.defi neClass(Un known Source)
at java.net.URLClassLoader.de fineClass( Unknown Source)
at java.net.URLClassLoader.ac cess$100(U nknown Source)
at java.net.URLClassLoader$1. run(Unknow n Source)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(Un known Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at sun.misc.Launcher$AppClass Loader.loa dClass(Unk nown Source)
at java.lang.ClassLoader.load Class(Unkn own Source)
at java.lang.ClassLoader.load ClassInter nal(Unknow n Source)
Question 2:
in java.exe command am I supposed to use -cp only with c:\ like java -cp c:\ something_here
or can I start the class path from anywhere say: java -cp c:\somedirstructure rest_of_command
Thanks
I have two questions here:
Question 1:
I also notice that if I do c:\somefolder and then try 3a, it fails,why?
e.g. I navigated to c:\Program Files and did the following two and got the same error
1).java -cp c:\a\b folder1.class1
2).java -cp c:\ a.b.folder1.class1
Error got:
C:\>java -cp c:\a\b folder1.class1
Exception in thread "main" java.lang.NoClassDefFoundE
name: a/b/folder1/class1)
at java.lang.ClassLoader.defi
at java.lang.ClassLoader.defi
at java.security.SecureClassL
at java.net.URLClassLoader.de
at java.net.URLClassLoader.ac
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
Question 2:
in java.exe command am I supposed to use -cp only with c:\ like java -cp c:\ something_here
or can I start the class path from anywhere say: java -cp c:\somedirstructure rest_of_command
Thanks
Well, the point to understand the issue is:
1. You should have some ROOT directory to build your class structure from, e.g. C:\JavaWork
2. CLASSPATH value points TO THIS root directory and thus allows the JVM (i.e. java.exe) to find the classes you attempt to execute (that is the meaning of the option: '-cp C:\JavaWork')
3. Your classes are organized in packages (simply put: folders) placed underneath the root, i.e. subfolders of C:\JavaWork
Suppose now you have written java class named MyClass (stored as MyClass.java file) and want it to belong to package 'a.b.c'. Then you will have to:
1. Create directory structure: C:\JavaWork\a\b\c (e.g. run: C:\> mkdir C:\JavaWork\a\b\c)
2. Place the following line as the very first line of code into your class code:
package a.b.c;
3. Save MyClass.java file in directory C:\JavaWork\a\b\c
(i.e. you should now have file: C:\JavaWork\a\b\c\MyClass. java)
Now you are ready to compile and run your java class:
1. You can compile your class staying anywhere as long as you specify to javac.exe the path where to find the java source file. Thus any of the following will work:
C:\JavaWork> javac .\a\b\c\MyClass.java
C:\> javac .\JavaWork\a\b\c\MyClass.j ava
D:\someFolder>javac C:\JavaWork\a\b\c\MyClass. java
Simply put: javac.exe (the compiler) is not concerned about any PACKAGES. It just finds and compiles the source file specified.
2. BUT java.exe (the interpreter) behaves in that point very differently: it insists that class to run is named with its fully specified name, i.e: 'a.b.c.MyClass' (in order to distinguish it from some another class, say x.y.z.MyClass)
Now, thus the class name is prefixed with 'a.b.c' then java.exe will try to look for the class file using RELATIVE path .\a\b\c\MyClass.class from some ROOT directory fetched by means of CLASSPATH value provided (i.e.: -cp C:\JavaWork).
Therefore in order to run your classes successfully you have TWO possibilities:
1.
a) Define environment variable named CLASSPATH holding 'C:\JavaWork' entry as well as all other ROOTS of your class file locations (like PATH for seeking exe files).
b) THEN you can run your class (and any of your classes) just by typing from anywhere:
*>java a.b.c.MyClass
2. Define a special (one-time) classpath value EVERY TIME you run your class. That is what you do typing commands like:
C:\JavaWork> java -cp . a.b.c.MyClass
C:\> java -cp .\JavaWork a.b.c.MyClass
D:\someFolder> java -cp C:\JavaWork a.b.c.MyClass
Hope this helps :-)
Avdej
1. You should have some ROOT directory to build your class structure from, e.g. C:\JavaWork
2. CLASSPATH value points TO THIS root directory and thus allows the JVM (i.e. java.exe) to find the classes you attempt to execute (that is the meaning of the option: '-cp C:\JavaWork')
3. Your classes are organized in packages (simply put: folders) placed underneath the root, i.e. subfolders of C:\JavaWork
Suppose now you have written java class named MyClass (stored as MyClass.java file) and want it to belong to package 'a.b.c'. Then you will have to:
1. Create directory structure: C:\JavaWork\a\b\c (e.g. run: C:\> mkdir C:\JavaWork\a\b\c)
2. Place the following line as the very first line of code into your class code:
package a.b.c;
3. Save MyClass.java file in directory C:\JavaWork\a\b\c
(i.e. you should now have file: C:\JavaWork\a\b\c\MyClass.
Now you are ready to compile and run your java class:
1. You can compile your class staying anywhere as long as you specify to javac.exe the path where to find the java source file. Thus any of the following will work:
C:\JavaWork> javac .\a\b\c\MyClass.java
C:\> javac .\JavaWork\a\b\c\MyClass.j
D:\someFolder>javac C:\JavaWork\a\b\c\MyClass.
Simply put: javac.exe (the compiler) is not concerned about any PACKAGES. It just finds and compiles the source file specified.
2. BUT java.exe (the interpreter) behaves in that point very differently: it insists that class to run is named with its fully specified name, i.e: 'a.b.c.MyClass' (in order to distinguish it from some another class, say x.y.z.MyClass)
Now, thus the class name is prefixed with 'a.b.c' then java.exe will try to look for the class file using RELATIVE path .\a\b\c\MyClass.class from some ROOT directory fetched by means of CLASSPATH value provided (i.e.: -cp C:\JavaWork).
Therefore in order to run your classes successfully you have TWO possibilities:
1.
a) Define environment variable named CLASSPATH holding 'C:\JavaWork' entry as well as all other ROOTS of your class file locations (like PATH for seeking exe files).
b) THEN you can run your class (and any of your classes) just by typing from anywhere:
*>java a.b.c.MyClass
2. Define a special (one-time) classpath value EVERY TIME you run your class. That is what you do typing commands like:
C:\JavaWork> java -cp . a.b.c.MyClass
C:\> java -cp .\JavaWork a.b.c.MyClass
D:\someFolder> java -cp C:\JavaWork a.b.c.MyClass
Hope this helps :-)
Avdej
ASKER
Avdej,
Kudos to you for 2 things:
2). you took pain to explain to me the concept as one who knows should(in my view, but in real world hardly is the case) giving examples that I always prefer, Examples were very concise!
1). By doing above, you have brought back the old flavor of experts exchange. This in total contrast to present when either I dont see too many responses or if I do, all I see is a 1 liner.
Please watch out for my new questions that will keep popping thru the week, weekend and beyond
Thanks again and God Bless
Roopesh
Kudos to you for 2 things:
2). you took pain to explain to me the concept as one who knows should(in my view, but in real world hardly is the case) giving examples that I always prefer, Examples were very concise!
1). By doing above, you have brought back the old flavor of experts exchange. This in total contrast to present when either I dont see too many responses or if I do, all I see is a 1 liner.
Please watch out for my new questions that will keep popping thru the week, weekend and beyond
Thanks again and God Bless
Roopesh
yes the site has deteriorated a bit over the years, I find myself contributing a lot less here these days.
java -classpath c:\ class1