如何从 cmd 运行带有多个类的 java 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20365885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:24:42  来源:igfitidea点击:

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

javacmd

提问by user3064068

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:

目前我正在寻找另一种从命令行运行我的 Java 程序的方法,而不是将它添加到 JAR 文件中。我的程序有以下几个类:

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

程序文件名-MyProgram
主类-Server1
第二类-ClientHandler
包名-Items
第三类-User1
第四类-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.

为了让用户 1 和用户 2 运行,包旁边的主类和客户端处理程序必须首先运行,因为它们是客户端类并且依赖于主类。

回答by McProgramming

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.

听起来您只需要打开多个命令提示符并按照您需要它们运行的​​顺序编译和运行它们。如果我误解了问题,请告诉我。

回答by Paul Samsotha

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

回答by Vidya

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 MyProgramwill reference Server1, etc.

你描述的那个顺序无关紧要。它们都被编译在一起,MyProgram并将引用Server1等。

回答by Joshua Wilson

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?

它可能比您现在想要解决的要多,但您可能想要考虑像Maven这样的构建系统。开始试用;我如何制作我的第一个 Maven 项目?

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

您可以使用它来预定义构建顺序,如果您想让它为您(或不)创建一个 jar。

回答by Muhammad Soliman

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 ;

您必须确保将 .class 文件的位置添加到类路径中。因此,如果它在当前文件夹中,则添加 . 到您的类路径。请注意,windows 类路径分隔符是一个分号 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"

  • 包名称:com.test

  • 班级名称:你好(有主)

  • Java 文件位于“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

请注意,您可以在编译时添加 -d 以指定类文件的输出目录

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

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

在 Windows 中,同样的事情也会起作用,我已经尝试过

Check out thisfrom Oracle official site

从 Oracle 官方网站查看此内容

回答by Ashwant Manikoth

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.

执行两个相互依赖的 Java 程序。(例如:Complex.java和Solution.java两个文件,其中Soultion.java依赖于Complex.java。所以要先编译Complex.java,然后Complex的class文件必须先链接Solution.java,再链接Solution。必须为输出执行类。)使用语法引用图像。

STEP 1:

第1步:

COMPILE Complex.java

编译 Complex.java

compiling Complex.java

编译 Complex.java

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

语法- 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)

(Solution.java 和 Complex.java 是 Linked.ie-Solution.java 调用 Complex.java)

STEP 2:

第2步:

COMPILE Solution.java

编译解决方案.java

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

通过链接 Complex.class 和链接 Complex.class 编译 Solution.java(上面在步骤 1 中创建)

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

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

STEP 3:

第 3 步:

EXECUTE THE Solution.class

执行 Solution.class

java -cp [path_of_second_class_created] [class_Name]

java -cp [path_of_second_class_created] [class_Name]

(created in Step 3) enter image description here

(在步骤 3 中创建) 在此处输入图片说明