File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class path magic

 
Amu mathi
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" ..
 
Amu mathi
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the mistake ..

This help.

java -cp ../../.. com.college.learn.Hello
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell,

I got my answer; so it is illegal and not allowed in Java.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic