为什么在Java 1.8上,-classpath选项无法工作?

huangapple go评论78阅读模式
英文:

Why -classpath option not working on java 1.8

问题

在我的系统上,

C:\Users\Ravi>java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

当我运行以下命令时,

C:\Users\Ravi>java -classpath="C:\Users\Ravi\jar\" -Dcom.android.monkeyrunner.bindir=..\framework -jar monkeyrunner-26.0.0-dev.jar
Unrecognized option: -classpath=C:\Users\Ravi\jar\
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

请告诉我我做错了什么。

英文:

on my system has

C:\Users\Ravi>java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

how when I run

C:\Users\Ravi>java -classpath="C:\Users\Ravi\jar\" -Dcom.android.monkeyrunner.bindir=..\framework -jar monkeyrunner-26.0.0-dev.jar
Unrecognized option: -classpath=C:\Users\Ravi\jar\
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Please let me know what I am doing wrong.

答案1

得分: 3

文档中提到:

--class-path classpath,-classpath classpath,或 -cp classpath
一个分号(;)分隔的目录、JAR 归档和 ZIP 归档列表,用于搜索类文件。

指定 classpath 会覆盖对 CLASSPATH 环境变量的任何设置。如果未使用类路径选项且未设置 classpath,则用户类路径包括当前目录(.)。

作为特殊便利,类路径元素如果包含星号()的基本名称,被视为等同于指定目录中所有扩展名为 .jar 或 .JAR 的文件列表。Java 程序无法区分这两种调用方式。例如,如果目录 mydir 包含 a.jar 和 b.JAR,则类路径元素 mydir/ 会扩展为 a.jar:b.JAR,只是 JAR 文件的顺序是未指定的。指定目录中的所有 .jar 文件,包括隐藏的文件,都包含在列表中。类路径条目由星号(*)组成,会扩展为当前目录中所有 jar 文件的列表。类似地,定义了 CLASSPATH 环境变量时也会进行扩展。在启动 Java VM 之前会发生任何类路径通配符扩展。Java 程序永远不会看到未经展开的通配符,除非通过查询环境(例如通过调用 System.getenv("CLASSPATH"))进行展开。

因此,你应该像这样使用(列出所有的 jar 文件):

java -classpath "C:\Users\Ravi\jar\a.jar;C:\Users\Ravi\jar\b.jar" -Dcom.android.monkeyrunner.bindir=..\framework -jar monkeyrunner-26.0.0-dev.jar

你还可以在这里阅读更多关于 classpath 的内容。

英文:

In documentation there is:

> --class-path classpath, -classpath classpath, or -cp classpath
A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.

>Specifying classpath overrides any setting of the CLASSPATH environment variable. If the class path option isn't used and classpath isn't set, then the user class path consists of the current directory (.).

>As a special convenience, a class path element that contains a base name of an asterisk () is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR . A Java program can't tell the difference between the two invocations. For example, if the directory mydir contains a.jar and b.JAR, then the class path element mydir/ is expanded to A.jar:b.JAR, except that the order of JAR files is unspecified. All .jar files in the specified directory, even hidden ones, are included in the list. A class path entry consisting of an asterisk (*) expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, is similarly expanded. Any class path wildcard expansion that occurs before the Java VM is started. Java programs never see wildcards that aren't expanded except by querying the environment, such as by calling System.getenv("CLASSPATH").

So you should rather have it like this (list all jars):

java -classpath "C:\Users\Ravi\jar\a.jar;C:\Users\Ravi\jar\b.jar" -Dcom.android.monkeyrunner.bindir=..\framework -jar monkeyrunner-26.0.0-dev.jar

To read more about classpath you can also look here.

huangapple
  • 本文由 发表于 2020年4月6日 20:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61059989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定