How do I run java program with multiple classes from 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.

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).


you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;

javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main

Example. Suppose you have the following

  • Package Named: com.test

  • Class Name: Hello (Having main)

  • Java file is located inside "src/com/test/Hello.java"

then, from outside directory:

$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello

Note that you can add -d to specify output directory of your class files whenever compiling

$ javac -d output_directory -cp . com/test/Hello

In windows the same thing will be working too, I already tried

Check out this from Oracle official site


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.


TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER. (for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java. So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.) REFER THE IMAGE WITH SYNTAX.

STEP 1:

COMPILE Complex.java

compiling Complex.java

syntax- javac -d [path_where_class_File_build] [path_of_the_file\filename.java]

(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)

STEP 2:

COMPILE Solution.java

compiling Solution.java with linking Complex.class with linking Complex.class(above created in step 1)

syntax- javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]

STEP 3:

EXECUTE THE Solution.class

java -cp [path_of_second_class_created] [class_Name]

(created in Step 3)


Need Your Help

View not loading up in iOS simulator for xCode

ios swift view sprite-kit ios-simulator

My problem is that whenever I try to load up the code I have below, a blank grey screen pops up with none of my sprites on it. There are no errors yet nothing will happen in the simulator.

how much data can i store per node in Neo4j

nosql neo4j

I need to save big chunks of Unicode text strings in Neo4j nodes. The documentation does not mention anything about the size of data one can store per node.