0

I'm trying to execute a simple java program ("HelloWorld") in command prompt without using the set path option or setting the system variable. Suppose the java program is in D:\My_Programs and the java executable files are in C:\Program Files\Java\jdk1.6.0_24\bin. Here's what I did to compile: C:\Program Files\Java\jdk1.6.0_24\bin>javac D:\My_Programs\HelloWorld.java It is creating a .class file but the same strategy for execution creates an exception: C:\Program Files\Java\jdk1.6.0_24\bin>java D:\My_Programs\HelloWorld

Exception in thread "main" java.lang.NoClassDefFoundError: D:\My_Programs\HelloW
orld
Caused by: java.lang.ClassNotFoundException: D:\My_Programs\HelloWorld
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: D:\My_Programs\HelloWorld.  Program will exit.

Can someone suggest on how to execute this file. Thanks in advance for your help.

The code:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
6
  • The location of the java and javac binaries is irrelevant once they're on your PATH. Show us the Java code in HelloWorld.java. Commented Oct 1, 2011 at 14:28
  • Did you read any tutorials about creating and running Java programs at all? Commented Oct 1, 2011 at 14:28
  • class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } Commented Oct 1, 2011 at 14:32
  • You don't want to post code in comments as it is impossible to read. I have edited your original post and have added your code there. Commented Oct 1, 2011 at 14:35
  • Sorry, edited the original post. Commented Oct 1, 2011 at 14:37

6 Answers 6

7

Please try this one:

C:\Program Files\Java\jdk1.6.0_24\bin>java -cp D:\My_Programs HelloWorld

or even that one:

C:\anywhere> C:\Program Files\Java\jdk1.6.0_24\bin\java -cp D:\My_Programs HelloWorld

The -cp tells the java executable where to look for the class HelloWorld. Giving a file-like argument D:\My_Programms\HelloWorld where Java assumes a pure packagename+classname will not work.

Sign up to request clarification or add additional context in comments.

3 Comments

So please vote up any answer (i.e. more then one) which seemed useful to you and accept the best one (i.e. click the V hook).
What about javac?
What about javac?
2

Since you were in the Java directory rather than the directory of your program when you ran javac the class file is probably there as well. That's generally a bad thing - you want javac and java to be in your path so you can execute them while you're in your program directory. And then you can execute the program using java HelloWorld

4 Comments

The HelloWorld.class file is generated in D:\My_Programs and I am not getting the point of using set path if its only purpose is for convenience. Then the above should work too.
If ou're not in the directory where the class file was generated, then you need to add that directory to your classpath, either by setting an environment variable or by specifying it in a -cp argument to java.
By the way - asking questions here instead of looking them up in the documentation is also "for convenience". If you want to do everything the hard way, I'm through answering your questions.
Sorry, I had read the documentation but failed to understand. I am new to java.
0

You can try this way java -cp "D:\My_Programs" HelloWorld,the precondition is that HelloWorld.java you compiled is a main class.

Comments

0
java -cp D:\My_Programs HelloWorld

Because the directory hierarchy from the class hierarchy to be considered.

Comments

0

You can also try with changing your directory in cmd by cd D:\My_Programs and then execute java HelloWorld. it will execute the file. The only pre condition is that class file should be present at that location.

Comments

0

Before running the .java file in cmd, rename the file to the class name only then will it work. For example in this case, save the notepad file as 'HelloWorld.java'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.