Solved

cant run the compiled class, is it classpath?

Posted on 2009-05-20
9
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 60

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 60

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

730 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