• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a java program with full path\class name

 
Matthew Scott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic