• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Running a java program with full path\class name

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic