• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class path magic

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the mistake ..

This help.

java -cp ../../.. com.college.learn.Hello
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell,

I got my answer; so it is illegal and not allowed in Java.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic