简体   繁体   English

Java 8 上 Windows 10 - CLASSPATH 不起作用

[英]Java 8 on Windows 10 - CLASSPATH does not work

Windows 10 1803 64-bit, Java JRE 8. No JDK installed. Windows 10 1803 64 位,Java JRE 8. 未安装 JDK。

I have a.jar in a folder:我在一个文件夹中有一个.jar:

c:\users\myuser\KickAssembler\KickAss.jar

Under System environment variables I have the CLASSPATH as:在系统环境变量下,我将 CLASSPATH 设置为:

c:\users\myuser\KickAssembler\

At a command prompt the CLASSPATH is set (checked with ECHO).在命令提示符处设置 CLASSPATH(使用 ECHO 检查)。

At a prompt when I try:在我尝试时出现提示:

java -jar KickAss.jar

It returns:它返回:

Error: Unable to access jarfile KickAss.jar

If I CD into the directory first, then run it, it works fine.如果我先 CD 进入目录,然后运行它,它工作正常。 So it looks like CLASSPATH is not being used somehow.所以看起来 CLASSPATH 没有以某种方式被使用。 I have added the location into the standard Windows PATH also, still doesn't work.我也将该位置添加到标准 Windows PATH 中,但仍然无法正常工作。

Different combinations of casing for the filename doesn't work.文件名大小写的不同组合不起作用。 Windows is set to show hidden files. Windows 设置为显示隐藏文件。 It can't be a permissions issue because it works when I change into the directory first.这不可能是权限问题,因为当我首先更改目录时它会起作用。

Anything else I can try?还有什么我可以尝试的吗?

CLASSPATH is used to define the path from which Java will load classes. CLASSPATH 用于定义 Java 将加载类的路径。 CLASSPATH can contain folders and JAR files, such as: C:\mybin;c:\myjars\some.jar This classpath would load any fully qualified class file residing in or beneath c:\mybin and any fully qualified class within c:\myjars\some.jar . CLASSPATH can contain folders and JAR files, such as: C:\mybin;c:\myjars\some.jar This classpath would load any fully qualified class file residing in or beneath c:\mybin and any fully qualified class within c:\ myjars\some.jar CLASSPATH will not allow JAVA to find your JAR file as you are expecting when using the -jar switch, it does not search for any JAR file along the classpath, it will only look in the ones explicitly stated in the CLASSPATH and then only for class files within them. CLASSPATH will not allow JAVA to find your JAR file as you are expecting when using the -jar switch, it does not search for any JAR file along the classpath, it will only look in the ones explicitly stated in the CLASSPATH and then only for class其中的文件。 Note, fully qualified means package + class , such as: com.myorg.somepackage.someclass , not just someclass .注意,完全合格的意思是package + class ,例如: com.myorg.somepackage.someclass ,而不仅仅是someclass

As you observed, if you are in the folder where kickass.jar resides your command line works as the JAR file is present.正如您所观察到的,如果您位于 kickass.jar 所在的文件夹中,则您的命令行会在 JAR 文件存在时工作。 If you fully reference the JAR file while executing the command from another folder the command line should work as well.如果您在从另一个文件夹执行命令时完全引用 JAR 文件,则命令行应该也能正常工作。

See https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html for more detail on how CLASSPATH works.有关 CLASS 工作原理的更多详细信息,请参阅https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.htmlCLPATH工作原理。 It is important to have a solid understanding of CLASSPATH when using JAVA.在使用 JAVA 时,对 CLASSPATH 有一个扎实的理解是很重要的。

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

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