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

后端 未结 6 1837
梦如初夏
梦如初夏 2020-12-05 01:11

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:

相关标签:
6条回答
  • 2020-12-05 01:46

    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)

    0 讨论(0)
  • 2020-12-05 01:51
    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
    
    0 讨论(0)
  • 2020-12-05 01:57

    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

    0 讨论(0)
  • 2020-12-05 02:01

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

    0 讨论(0)
  • 2020-12-05 02:07

    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.

    0 讨论(0)
  • 2020-12-05 02:08

    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.

    0 讨论(0)
提交回复
热议问题