How do I run java program with multiple classes from cmd?

Viewed 96218

At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:

The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2

The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.

6 Answers
javac *.java // compliles all java files in the dir

java MyClass // runs the particular file

If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.

If your files are packaged, then something like this

javac com.mypackage/.*java

java com.mypackage.MyClass

Once you compile your code, you then run this from the top level:

java -cp . com.myprogram.MyProgram

That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.

It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?

You can use it to predefine the build order and if you want have it create a jar for you (or not).

Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.

Related