user963241
user963241

Reputation: 7038

Running this from command line in Java

I cannot run the following program from command line the usual way:

package animal_package;

public class my_animal {

    public static void main(String[] args) {

        System.out.println("Hello animal");
    }
}

E.g. from Command prompt: I go to "D:\Java\src\animal_package" where my java program is and compile it:

D:\Java\src\animal_package>javac my_animal.java  
D:\Java\src\animal_package>java my_animal  
Error: Could not find or load main class my_animal.java

I have looked on Google and came around class path problem but couldn't make any sense from it all.

Which command line would be correct in my case?

Upvotes: 0

Views: 82

Answers (3)

You can compile the class either from src directory using: javac animal_package\my_animal.java OR from animal_package directory using: javac my_animal.java

To run the program from src directory use java animal_package.my_animal OR java -cp . animal_package.my_animal

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201409

cd .. then java -cp . animal_package.my_animal. The package is part of the fully qualified class name. Or,

java -cp .. animal_package.my_animal

Upvotes: 2

Joe C
Joe C

Reputation: 15684

To compile:

javac my_animal.java

To run (from src directory):

java animal_package.my_animal

Upvotes: 3

Related Questions