perfect_tranquility
asked on
cant run the compiled class, is it classpath?
The code as below is in c:\temp\temp\temp
I was able to compile it:
c:\ > javac c:\temp\temp\temp\class1.j ava
but when I try to run that class file with
c:\> java c:\temp\temp temp.class1
it gives out this error stack.
C:\>java -cp c:\temp\temp temp.class1
Exception in thread "main" java.lang.NoClassDefFoundE rror: temp/class1 (wrong na
me: temp/temp/temp/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$000(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)
Could not find the main class: temp.class1. Program will exit.
I am at a loss to know why?
Thanks!
Roopesh
I was able to compile it:
c:\ > javac c:\temp\temp\temp\class1.j
but when I try to run that class file with
c:\> java c:\temp\temp temp.class1
it gives out this error stack.
C:\>java -cp c:\temp\temp temp.class1
Exception in thread "main" java.lang.NoClassDefFoundE
me: temp/temp/temp/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
Could not find the main class: temp.class1. Program will exit.
I am at a loss to know why?
Thanks!
Roopesh
package temp.temp.temp;
public class class1{
public static void main(String[]arg)
{
System.out.println("Hello");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
btw now I have been able to run the class from anywhere in filesystem
1).C:\>java -cp c:\ temp.temp.temp.class1
2).C:\Program Files\Apoint>java -cp c:\ temp.temp.temp.class1
Both 1) and 2) above worked.
My question now essentially becomes :
why do I have to put in the entire class name along with packages if I use classpath. In other words if I navigate somewhere in file system why am I not allowed to do this:
C:\Program Files\Apoint> java -cp c:\temp\temp temp.class1
Thanks!
Roopesh
1).C:\>java -cp c:\ temp.temp.temp.class1
2).C:\Program Files\Apoint>java -cp c:\ temp.temp.temp.class1
Both 1) and 2) above worked.
My question now essentially becomes :
why do I have to put in the entire class name along with packages if I use classpath. In other words if I navigate somewhere in file system why am I not allowed to do this:
C:\Program Files\Apoint> java -cp c:\temp\temp temp.class1
Thanks!
Roopesh
ASKER
fox, visa: you both are right. I do not remember where from I copied that code.
This is the code that I am working with and getting the issues I outlined in the comment above
package temp.temp.temp;
public class class1{
public static void main(String[]arg)
{
System.out.println("Hello" );
}
}
Thanks and sorry for that wrong code copied.
This is the code that I am working with and getting the issues I outlined in the comment above
package temp.temp.temp;
public class class1{
public static void main(String[]arg)
{
System.out.println("Hello"
}
}
Thanks and sorry for that wrong code copied.
ASKER
omg, my brain is not functioning properly i guess.
i see what you two are saying, you want me to change the package line, but plz explain what is wrong with
package temp.temp.temp;
when after all the class itself is in c:\> temp\temp\temp ?
Thanks
i see what you two are saying, you want me to change the package line, but plz explain what is wrong with
package temp.temp.temp;
when after all the class itself is in c:\> temp\temp\temp ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That is a much better explanation. I haven't had any coffee today, so struggling. :) lol
Hopefully you get the drift though. You have to make your package name match how you are calling the class or make how you are calling match your package name. Have to be consistent.
Hopefully you get the drift though. You have to make your package name match how you are calling the class or make how you are calling match your package name. Have to be consistent.
ASKER
visa: No, I dint copy that code. weird names like temp.temp.temp would usually be mine,lol
coffee i cant drink coz of medical issues, gave me runs for 2 days!
I am very positive I have gotten the point you made and forced it through the hippocampus at least.
Thanks a b for your link, I appreciate it. I would look at it for sure later when I go back home.
fox, you are right too, i just want a bit more text like visa :-) or a link where I can satiate my hunger for textual example.
Thanks all!
Roopesh
coffee i cant drink coz of medical issues, gave me runs for 2 days!
I am very positive I have gotten the point you made and forced it through the hippocampus at least.
Thanks a b for your link, I appreciate it. I would look at it for sure later when I go back home.
fox, you are right too, i just want a bit more text like visa :-) or a link where I can satiate my hunger for textual example.
Thanks all!
Roopesh
Or change your execution to this:
c:\> java -cp . temp.temp.temp.class1
Open in new window