Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

cmd

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.

like image 536
user3064068 Avatar asked Dec 04 '13 02:12

user3064068


People also ask

How do I run a Java program in two classes?

It is time to create our main method. Go back to the drop down menu called "new" and, once again, pick "Class." This time, check the box that says "public static void main(String[]args)." This indicates to Eclipse that you want to create a main method. Give your main class a name and click finish.

How do I run a .class file in CMD?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How to run a Java program in CMD?

We must follow the steps given below to run a Java program. Open the notepad and write a Java program into it. Save the Java program by using the class name followed by .java extension. Open the CMD, type the commands and run the Java program. Let's create a Java program and run it using the Command Prompt.

How to use multiple classes in a Java program?

Using multiple classes in a Java program. A Java program can contain any number of classes. Following Java program comprises of two classes: Computer and Laptop. Both classes have their constructors and a method. In the main method, we create objects of two classes and call their methods. class Computer {. Computer() {.

Should I compile all files before running a Java program?

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. My three hours were wasted before I chanced upon the above.

Which class has to run first in a Java package?

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. Run java classname.


1 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
like image 178
Paul Samsotha Avatar answered Oct 24 '22 17:10

Paul Samsotha