类路径无效标志 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29915963/
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-11 08:49:04  来源:igfitidea点击:

Classpath invalid flag - Java

javacompiler-errorsjavac

提问by tlorin

I am using javacto compile a file called Rengine.jar.

我正在使用javac编译一个名为Rengine.jar.

I tried:

我试过:

javac –classpath ./REngine.jar
javac –cp ./REngine.jar
javac ./REngine.jar
javac –classpath REngine.jar

And here are the errors that I got:

这是我得到的错误:



javac: invalid flag: –classpath
Usage: javac <options> <source files>
use -help for a list of possible options


javac: invalid flag: –cp
Usage: javac <options> <source files>
use -help for a list of possible options


javac: invalid flag: ./REngine.jar
Usage: javac <options> <source files>
use -help for a list of possible options


javac: invalid flag: –classpath
Usage: javac <options> <source files>
use -help for a list of possible options

I am not familiar with Java at all and I could not find an answer to this problem with a Google search. I precise that I am in the good directory and that I am using Mac OSX. My Java version is 1.8.0_20. Any advice would be much appreciated! :)

我根本不熟悉 Java,我无法通过 Google 搜索找到这个问题的答案。我确切地说我在好目录中并且我使用的是 Mac OSX。我的 Java 版本是 1.8.0_20。任何建议将不胜感激!:)

采纳答案by KernelKoder

Firstly, you cannot compile a .jarfile. A .jarfile is a collection of complied .javafiles (.classfiles). You are however able to run a .jarfile if your manifest file is set up correctly.

首先,您不能编译.jar文件。一个.jar文件是符合的集合.java文件(.class文件)。但是,.jar如果您的清单文件设置正确,您就可以运行文件。

The procedure is as follws:
Compile:

程序如下:
编译:

javac MyClass.java 

the above line creates MyClass.class

上面的行创建 MyClass.class

Create JAR with manifest file:

使用清单文件创建 JAR:

jar cfe myJar.jar myClass myClass.class  

Run JAR:

运行 JAR:

java -jar myJar.jar

回答by SubOptimal

javac - the Java source compiler, will compile the Java source file *.javainto JVM bytecode the class file *.class

javac - Java源代码编译器,将Java源文件编译*.java成JVM字节码的class文件*.class

To execute a Java program you need to call a class file with java YourClass.

要执行 Java 程序,您需要使用java YourClass.

The Jar file is a Java archivefile. Which contains other files. For example class files. If you want to run a class from within the Jar file there a two way.

Jar 文件是一个Java archive文件。其中包含其他文件。例如类文件。如果你想从 Jar 文件中运行一个类,有两种方法。

executable Jar

可执行 Jar

java –jar REngine.jar

This depends on the manifest file inside the Jar, which defines the main class which should be executed.

这取决于 Jar 中的清单文件,它定义了应该执行的主类。

class inside a Jar file

Jar 文件中的类

java –cp REngine.jar package.YourClass

This would execute the class package.YourClass(assuming the class has a mainmethod).

这将执行该类package.YourClass(假设该类有一个main方法)。