user3570803
user3570803

Reputation: 5

Running a simple Java Program in Command Prompt error

I am trying to run my Example2.class program in windows 7 64-bit command prompt. I used command prompt already to compile the program, but when type: "java Example2" it gives me an error saying could not find or load main class example2. How do I set the right path to my file so that it can find it? Thanks

Upvotes: 0

Views: 1698

Answers (2)

YourHelper
YourHelper

Reputation: 735

When you write a class you can save the file as : MyClass.java and then execute this commands in this directory:

javac MyClass.java

which will compile the class and then create automatically the file: MyClass.class (If compilation ended without errors)

and then to run this SPECIFIC class execute the command:

java Myclass

Upvotes: 0

chiastic-security
chiastic-security

Reputation: 20520

You need to give it the full package name, and (unless you change the class path) you need to be in the right directory. If the full package name is

com.something.Example2

then you'd expect the compiler to produce a file like this:

com/something/Example2.class

If you make sure you're in the directory immediately above com (i.e., you can see com when you do a directory listing), then you can run it with

java com.something.Example2

Note that it's case sensitive.

If you used the default package (i.e., the full class name really just is Example2) then you need to be in the directory containing Example2.class, and then you run

java Example2

But using the default package is discouraged.

The biggest thing you could do to help yourself out is to use an IDE (Eclipse or NetBeans are the most commonly used ones). As soon as you start to write anything at all large or complex, compiling and running from the command line without an IDE will cause you to claw your own eyes out.

Upvotes: 2

Related Questions