File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package ?" Watch "package ?" New topic
Author

package ?

Jeffry Cray
Greenhorn

Joined: Oct 08, 2000
Posts: 25
I created a class called SecondClass which compiles fine but when I run the class a run time error appears !! why ?
c:\pack\SecondClass.java
c:\pack\SecondClass.class
package pack;
public class SecondClass {
public static void main(String[] args) { System.out.println("Hi"); }
}
I noticed the reson was becuase of the "package pack;" line at the beginning of the programm....Becuase when I comment the line "package pack;", all compiles and runs fine.
But when I leave the line "package pack;" uncommented I get this VERY LONG error line !!!
C:\pack>java SecondClass
Exception in thread "main" java.lang.NoClassDefFoundError: pack (wrong name: test/pack)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass (Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal (ClassLoader.java:313)
Why is this happening and how do I make things work ? Any feedback !!
sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Hi Jeffry,
Did you set your class path correctly?
NoClassDefFoundError is usually caused by wrogn class path.
when you deal with package, class path is important as you probably know..
Hope this helps.
Sean.


Sean <br />SCJP2, SCJP2p1.4, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Jeffry,
To run you class, you run it using one of the following commands:
<pre>C:\>java pack.SecondClass</pre>
or
<pre>C:\pack\>java -classpath ".." pack.SecondClass</pre>
Hope this solves the problem.
Regards,
Beno�t
Jeffry Cray
Greenhorn

Joined: Oct 08, 2000
Posts: 25
Hi,
Thanks for solving my problem. I did exactly as you said and it worked!! But beileve it or not, I don't know much about classpath variable. Could you tell me of a web site or a booklette that explaincs exactly what is a "classpath" and a "path" variable and how I can make use of it for java ?
Thanks again
Originally posted by Beno�t d'Oncieu:
Jeffry,
To run you class, you run it using one of the following commands:
<pre>C:\>java pack.SecondClass</pre>
or
<pre>C:\pack\>java -classpath ".." pack.SecondClass</pre>
Hope this solves the problem.
Regards,
Beno�t

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Jeffry,
CLASSPATH is one of a number of DOS environment variables. It's normally set in the AUTOEXEC.BAT file which runs whenever you open a DOS-prompt window on Win95.
A 'path' simply refers to the drive and directory on your system where files are found. For example, if you have JDK 1.3 installed on the C: drive in directory JDK1.3 the path would be C:\JDK1.3
The current directory is referenced using a dot (.)
To see how the environment variables on your system are set, type: SET at a DOS prompt ... you should see a list of environment variables. You may or may not see one called CLASSPATH ... depends on your system setup.
If one doesn't exist you can create one by typing something like:
<pre>
set classpath .;c:\myjava
</pre>
It will only be good for the current DOS session (you'd need to reenter it the next time you opened a DOS window).
CNET has a DOS help/how-to tutorial at http://www.help.com/cat/2/191/585/hto/121/1.html?tag=st.hp.cat.bb which should give you more info.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited November 01, 2000).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jeffry Cray
Greenhorn

Joined: Oct 08, 2000
Posts: 25
Thanks alot Jane !!
Your very nice and I'm really sorry for the late reply... I was SO SO busy lately
Thanx again
[This message has been edited by Jeffry Cray (edited November 04, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package ?
 
Similar Threads
Interface - AbstractClass Collision
main() & package question
java command error
classpath trouble
import and package