aspose file tools*
The moose likes Beginning Java and the fly likes Running a java program with full path\class name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a java program with full path\class name" Watch "Running a java program with full path\class name" New topic
Author

Running a java program with full path\class name

Matthew Scott
Greenhorn

Joined: Jun 19, 2008
Posts: 4
From the following directory:

C:\Program Files\Java


I can compile my Java program using either of the following two commands:

javac "C:\Program Files\Java\jdk1.6.0_21\bin\HelloRanch.java"

javac "jdk1.6.0_21\bin\HelloRanch.java"


However, despite the fact that the program runs... which I can accomplish while in the same directory, typing:

java HelloRanch


I am not able to run the program if I try to explicitly name the full path and filename, such as:

java "C:\Program Files\Java\jdk1.6.0_21\bin\HelloRanch"

java "jdk1.6.0_21\bin\HelloRanch"


using either of these, and I will get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: C:\Program Files\Java\jdk1/6/0_21\bin\HelloRanch
Caused by: java.lang.ClassNotFoundException: C:\Program Files\Java\jdk1.6.0_21\bin\HelloRanch
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)
Could not find the main class: C:\Program Files\Java\jdk1.6.0_21\bin\HelloRanch. Program will exit.


Because I couldn't be sure if part of the problem was the way the jdk directory was displayed on the first line of the error ("\jdk1/6/0_21"),
I moved to the directory:

C:\Program Files\Java\jdk1.6.0_21

and then typed:

java "\bin\HelloRanch"

... but I also received essentially the same error.




Is there a way to execute a java program using the full directory name? If it's part of the java command options, I missed it.


Any help that could be given would be greatly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

Matthew Scott wrote:Is there a way to execute a java program using the full directory name?


No, there isn't. The "java" command uses the classpath to find the class you specify.
Matthew Scott
Greenhorn

Joined: Jun 19, 2008
Posts: 4
Thank you very much!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

and a quick note...it is generally a bad idea to have your .java files in the same place as the jdk bin directory. you really want to create your own directory for your java stuff.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a java program with full path\class name