Link to home
Start Free TrialLog in
Avatar of perfect_tranquility
perfect_tranquilityFlag for United States of America

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.java

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.NoClassDefFoundError: temp/class1 (wrong na
me: temp/temp/temp/class1)
        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$000(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)
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");
 
}
 
}

Open in new window

SOLUTION
Avatar of the_b1ackfox
the_b1ackfox
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin Cross
Change your package name to this (see code snippet).

Or change your execution to this:

c:\>  java -cp . temp.temp.temp.class1

package temp;
 
public class class1{
 
public static void main(String[]arg)
{
System.out.println("Hello");
 
}
 
}

Open in new window

Avatar of perfect_tranquility

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
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.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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