Solved

cant run the compiled class, is it classpath?

Posted on 2009-05-20
9
194 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:perfect_tranquility
9 Comments
 
LVL 9

Assisted Solution

by:the_b1ackfox
the_b1ackfox earned 50 total points
ID: 24434703
It's your package name...  remove it and it should work
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24434729
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

0
 

Author Comment

by:perfect_tranquility
ID: 24434747
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
0
 

Author Comment

by:perfect_tranquility
ID: 24434791
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:perfect_tranquility
ID: 24434871
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
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 325 total points
ID: 24434875
You have to fully qualify the class name which includes all the package names.  Your class path tells java where to look for compiled classes.  When you choose c:\temp\temp you are telling it to look in that folder.  No matter what you do though, the compiled class name of class1 is temp.temp.temp.class1 and since package names are treated like folder names, java will think you want this:

c:\temp\temp\temp\temp\temp\class1.class

That is why when you say to look in root of c:\ for classes, you correctly find your class:

c:\temp\temp\temp\class1.class

Since your package name tells it which folders to go down.

A package name of temp.temp.temp makes no logical sense, so you probably copied code from somewhere that was giving an example.  You would want to replace those temps with something that makes sense for you like.

com.tranquility.perfect.Class1 <-- note also that class names should begin with uppercase.
0
 
LVL 20

Assisted Solution

by:a_b
a_b earned 125 total points
ID: 24434951
Look the at this link for the explanation http://www.jarticles.com/package/package_eng.html.
Check out the section on packaging.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24435029
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.
0
 

Author Comment

by:perfect_tranquility
ID: 24435104
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Java asynchronous logging 4 50
Groovy:unable to resolve class error 2 68
Oracle SQL syntax check  without executing 6 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now