Solved

cant run the compiled class, is it classpath?

Posted on 2009-05-20
9
195 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

770 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