Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cant run the compiled class, is it classpath?

Posted on 2009-05-20
9
Medium Priority
?
212 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1300 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 500 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

610 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