简体   繁体   English

如何在不使用任何软件(Windows)的情况下通过命令行执行大型编译的 Java 项目?

[英]How to execute a large compiled Java project via command line, without using any software (Windows)?

I have a Java project with 5 packages and 30 classes.我有一个包含 5 个包和 30 个类的 Java 项目。 I want to test this project on a different computer, but I can't install any sotware on that computer so I can't use things like Maven, Eclipse etc. Is there a way I can execute the program on that computer?我想在另一台计算机上测试这个项目,但我无法在该计算机上安装任何软件,因此我无法使用 Maven、Eclipse 等软件。有没有办法在该计算机上执行该程序?

What I tried to do, is to compile the project using Eclipse on my computer, then went to the other computer and tried to execute the project main class via the folder that the main class .class file is at.我试图做的是在我的计算机上使用 Eclipse 编译项目,然后转到另一台计算机并尝试通过主类.class文件所在的文件夹执行项目主类。
IE, say that the main class name is Hello in package Greetings and Hello.class is at folder named folder . IE,假设主类名是Hello包中的GreetingsHello.class位于名为folder So I opened the command line window at folder and typed the command:所以我在folder打开命令行窗口并输入命令:

java Greetings.Hello

That didn't work....那没有用....
Edit: After doing this I got the message: Error: Could not find or load main class Greetings.Hello编辑:执行此操作后,我收到消息: Error: Could not find or load main class Greetings.Hello

If the package name is Greetings and you want to run Hello.class如果包名是 Greetings 并且你想运行 Hello.class

  1. Hello class must have main method. Hello 类必须有 main 方法。

  2. Hello.class must in folder name Greetings (package name). Hello.class 必须在文件夹名称 Greetings(包名称)中。

  3. Execute java Greetings.Hello from the one level above of Greetings folderGreetings文件夹的上一级执行java Greetings.Hello

    It seems to me Hello.class is not inside of Greetings folder在我看来 Hello.class 不在Greetings文件夹中

If javac is installed on the system you can directly compile on the system.如果系统上安装了javac则可以直接在系统上进行编译。 you can compile even large projects including many packages with choosing different options provided by javac.您甚至可以编译包含许多包的大型项目,并选择 javac 提供的不同选项。

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. javac 工具读取用 Java 编程语言编写的类和接口定义,并将它们编译成字节码类文件。 It can also process annotations in Java source files and classes.它还可以处理 Java 源文件和类中的注释。

There are two ways to pass source code file names to javac:有两种方法可以将源代码文件名传递给 javac:

For a small number of source files, simply list the file names on the command line.对于少量源文件,只需在命令行上列出文件名即可。 For a large number of source files, list the file names in a file, separated by blanks or line breaks.对于大量源文件,列出文件中的文件名,用空格或换行符分隔。 Then use the list file name on the javac command line, preceded by an @ character.然后在 javac 命令行上使用列表文件名,以 @ 字符开头。 Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class.源代码文件名必须有 .java 后缀,类文件名必须有 .class 后缀,源文件和类文件都必须有标识类的根名称。 For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.例如,一个名为 MyClass 的类将写入名为 MyClass.java 的源文件中,并编译为名为 MyClass.class 的字节码类文件。

Inner class definitions produce additional class files.内部类定义产生额外的类文件。 These class files have names combining the inner and outer class names, such as MyClass$MyInnerClass.class.这些类文件的名称结合了内部和外部类名,例如 MyClass$MyInnerClass.class。

You should arrange source files in a directory tree that reflects their package tree.您应该在反映其包树的目录树中排列源文件。 For example, if you keep all your source files in C:\\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\\workspace\\com\\mysoft\\mypack\\MyClass.java.例如,如果您将所有源文件保存在 C:\\workspace 中,则 com.mysoft.mypack.MyClass 的源代码应位于 C:\\workspace\\com\\mysoft\\mypack\\MyClass.java 中。

By default, the compiler puts each class file in the same directory as its source file.默认情况下,编译器将每个类文件放在与其源文件相同的目录中。 You can specify a separate destination directory with -d source您可以使用 -d source指定单独的目标目录

Given that you have Eclipse and you ran the code in Eclipse: the quickest way is to use Eclipse and export it to executable JAR.鉴于您有 Eclipse 并且您在 Eclipse 中运行代码:最快的方法是使用 Eclipse 并将其导出到可执行 JAR。

If you have Run Configuration (eg named Hello) that you use for running the code:如果您有用于运行代码的运行配置(例如命名为 Hello):

  1. Menu -> Export -> Runnable JAR file菜单 -> 导出 -> 可运行的 JAR 文件
  2. Launch Configuration: Hello启动配置:你好
  3. Select export destination: (eg C:\\tmp\\Hello.jar)选择导出目标:(例如 C:\\tmp\\Hello.jar)
  4. Set Extract required libraries into generated JAR设置将所需的库提取到生成的 JAR 中
  5. Click finish.单击完成。

This will create Hello.jar file you can execute typing in: java -jar Hello.jar这将创建 Hello.jar 文件,您可以执行键入:java -jar Hello.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM