如何从 cmd 运行带有多个类的 java 程序? [英] How do I run java program with multiple classes from cmd?
问题描述
目前我正在寻找另一种从命令行运行 Java 程序的方法,而不是将它添加到 JAR 文件中.我的程序有以下几个类:
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:
程序文件名 - MyProgram
主类 - Server1
第二类 - 客户端处理程序
包裹名称 - 项目
第三类 - User1
第四类 - User2
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
包旁边的主类和客户端处理程序必须首先运行,以便用户 1 &用户 2 运行,因为它们是客户端类并且依赖于主类.
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.
推荐答案
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
这篇关于如何从 cmd 运行带有多个类的 java 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!