File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class path magic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class path magic" Watch "Class path magic" New topic
Author

Class path magic

Amu mathi
Greenhorn

Joined: Jul 04, 2008
Posts: 26
Hello,

Iam trying Hello world program in windows enviroment, I use the default package for running this program so it works fine in the following command

such as java -cp . Hello

If i use a package such as com.college.learn

Then I compile the file using command javac -d Hello.java it creates a folder structure as com\college\learn and places a class file Hello in that folder

Now to execute that class file from the base folder ie c:

I use the following command java -cp . com.college.learn.Hello

But when I try to run the same command from the folder cd com\college\learn I get a error stack.

Can anyone tell me how can i execute the program from the the folder com\college\learn
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
hi

Keep the Base folder selected in the Editor you are using .and run using the basic command java File name without extension i.e(.java)..

Hope this helps


Acquire the unexpected.......
Preparing for SCJP 6
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

java -cp . com.college.learn.Hello


When you are in same directory as that of package , no need to give package name , just type "java Hello" ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Amu mathi
Greenhorn

Joined: Jul 04, 2008
Posts: 26
Thanks for the inputs

In the same folder ie com\college\learn when I try running

c:\ cd com\college\learn

Then type

Java Hello

I get the following error

Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: com/college/learn/Hello)
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)
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Sorry for the mistake ..

This help.

java -cp ../../.. com.college.learn.Hello
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Welcome to JavaRanch.

If you have a package declaration in the class file, execute it from the "root" folder with java com.college.learn.Hello

You can miss out the "-cp ." You are not supposed to run it from the included folders.
Amu mathi
Greenhorn

Joined: Jul 04, 2008
Posts: 26
Thanks Campbell,

I got my answer; so it is illegal and not allowed in Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Originally posted by Amu mathi:
Thanks Campbell,

I got my answer; so it is illegal and not allowed in Java.
You're welcome. I don't know whether it is actually "illegal;" you would have to look through the specifications for the java.exe program to find that out. But it never seems to work when I try it!
 
jQuery in Action, 2nd edition
 
subject: Class path magic