How to run a java program with several classes from cmd?

I'm currently looking for another way to run my Java program from the command line, except to add it to the JAR file. My program has the following number of classes:

Program File Name - MyProgram
Main class - Server1
second class - Client Handler
Package Name - Elements
3rd grade - User1
4th grade - User2

The main class and the client handler together with the package must be launched first of all to start user 1 and user 2, since they are client classes and depend on the main class.

+6
source share
6 answers
javac *.java // compliles all java files in the dir java MyClass // runs the particular file 

If one class depends on another class that has not yet been compiled, the program will not run. Therefore, before attempting to run a program that depends on other files, all files must be compiled.

If your files are packed, then something like this

 javac com.mypackage/.*java java com.mypackage.MyClass 
+11
source

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

 java -cp . com.myprogram.MyProgram 

This order that you describe does not matter. All of them will compile together, and MyProgram will refer to Server1 , etc.

+3
source

It may be more than what you want to do right now, but you might want to consider a build system like Maven . To start trying out; How do I make the first Maven project?

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

+2
source

you must make sure that you add the location of your .class file to your class path. So, if you add it to the current folder. to your class path. Note that the window classpath separator is a semicolon, i.e.

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

An example . Suppose you have the following

  • Named package: com.test

  • Class Name: Hello (with primary)

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

then from the external directory:

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

Note that you can add -d to specify the output directory of your class files at compilation

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

The windows will work the same too, I've already tried

Check out this from the official Oracle site.

+2
source

It looks like you just need to open some command commands and compile them and run them in the order you need them. Let me know if I misunderstood the question.

0
source

EXECUTE TWO JAVA PROGRAMS, WHICH DEPEND ON OTHER OTHERS. (for example: two Complex.java and Solution.java files, where Soultion.java depends on Complex.java. Therefore, Complex.java should be compiled first, and then the Complex class file must be linked to Solution.java, and then to exit it must be executed by Solution.class.) PLEASE IMAGE WITH SYNTACIS.

STEP 1:

COMPILE Complex.java

Compilation Complex.java

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

(Solution.java and Complex.java are connected, i.e. -Solution.java calls Complex.java)

STEP 2:

COMPILE Solution.java

compilation of Solution.java with the link Complex.class with the binding 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 Solution.class

java -cp [path_of_second_class_created] [class_Name]

(created in step 3) enter image description here

0
source

Source: https://habr.com/ru/post/959298/


All Articles