Solved

cant run the compiled class, is it classpath?

Posted on 2009-05-20
9
193 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

14 Experts available now in Live!

Get 1:1 Help Now