0

I have created a class file HelloWorld.class which is in "C:\myData" using javac.exe.

At command prompt I am at C:\Program Files\Java\jdk1.8.0_91\bin> since I have my java.exe here.

When I execute java C:\myData\HelloWorld it gives me the following error :

C:\Program Files\Java\jdk1.8.0_91\bin> java C:\myData\HelloWorld

Error: Could not find or load main class C:\myData\HelloWorld

Can some one please assist.

How should I specify the path of my HelloWorld.class file to java.exe. (I wish to resolve this without setting classpath or any environment variables).

Below is my HelloWorld.java code :

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World");
    }

}

Regards.

BlueStar
  • 59
  • 1
  • 6

2 Answers2

1

You should do it the other way around, go to the directory where you have HelloWorld.class and do

cd C:\myData\HelloWorld
"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" HelloWorld
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • Not working ...C:\myData>C:\Program Files\Java\jdk1.8.0_91\bin\java.exe HelloWorld 'C:\Program' is not recognized as an internal or external command, operable program or batch file. – BlueStar May 16 '16 at 19:41
  • Updated, you need to enclose the java path in double quotes, like this: `"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" HelloWorld` – Krzysztof Krasoń May 16 '16 at 19:42
  • Thanks..thanks...thanks...krzyk.... :) ...got the output ...feeling like a coder :) :) ...just one little thing ...why do we need quotes... – BlueStar May 16 '16 at 19:49
  • It's windows thing, if there is a space in the directory, command prompt assumes you are adding parameters to the command, so to ease implementation they came up with the double quotes – Krzysztof Krasoń May 16 '16 at 19:51
  • Thank you very much for taking out time and replying. – BlueStar May 16 '16 at 19:52
0

You may want to double-check the contents in HelloWorld.java. Do you have a public static void main method?

bigMC28
  • 421
  • 4
  • 12
  • yes .. i have ..public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } – BlueStar May 16 '16 at 19:30