java 当我尝试执行我的 jar 时,为什么会出现“无法访问 jarfile”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28496575/
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
Why I obtain this "Unable to access jarfile" error when I try to execute my jar?
提问by AndreaNobili
I have the following problem trying to execute a jar file (created from my project) into the Windows shell (the DOS prompt).
我在尝试将 jar 文件(从我的项目创建)执行到 Windows shell(DOS 提示符)时遇到以下问题。
I have a file named Main.jar. If I unzip this file it contains a mainPkgfolder (that is the name of the package containing the Mainclass that itself contains the main()method).
我有一个名为Main.jar的文件。如果我解压此文件,它会包含一个mainPkg文件夹(即包含Main类的包的名称,该类本身包含main()方法)。
So into the mainPkgfolder there is the Main.classfile that contains the main()method.
所以在mainPkg文件夹中有包含main()方法的Main.class文件。
Ok so, from the shell, I go into the directory that contains the Main.jarand I perform:
好的,从 shell,我进入包含Main.jar的目录并执行:
C:\Projects\edi-sta\build\jar>java -jar mainPkg.Main.jar
Error: Unable to access jarfile mainPkg.Main.jar
But as you can see I obtain the Unable to access jarfile mainPkg.Main.jar. Why? What am I missing? How can I solve this issue and execute my Main.jarfile?
但是正如您所看到的,我获得了Unable to access jarfile mainPkg.Main.jar。为什么?我错过了什么?如何解决此问题并执行我的Main.jar文件?
回答by K Erlandsson
The syntax for executing a class containing a main method in a jar is:
在 jar 中执行包含 main 方法的类的语法是:
java -classpath <jarFile> <class>
In your case:
在你的情况下:
java -classpath Main.jar mainPkg.Main
If you want to execute the jar using java -jar
you must create an executable jar file. That can be done in different ways depending on which build tools you use.
如果要使用 jar 执行,则java -jar
必须创建一个可执行的 jar 文件。这可以通过不同的方式完成,具体取决于您使用的构建工具。
回答by Arkantos
Basically you've two types of JARs
基本上你有两种类型的 JAR
- Normal JAR - to package your classes into a single archive
- Runnable JAR - This is similar to Normal jar except that you can run this with
java -jar
command like thisjava -jar RunnableMain.jar
. In this one we already configure the class having main(),so no need to pass the class name in jar command
- 普通 JAR - 将您的类打包到单个存档中
- Runnable JAR - 这与普通 jar 类似,不同之处在于您可以使用这样的
java -jar
命令运行它java -jar RunnableMain.jar
。在这个我们已经配置了具有 main() 的类,所以不需要在 jar 命令中传递类名
Assuming that yours is a normal JAR, you can execute your class of interest like this
假设你的是一个普通的 JAR,你可以像这样执行你感兴趣的类
C:\Users\arkantos\work>java -classpath C:\Project\Main.jar mainPkg.Main
Notice that i've mentioned the absolute path of the JAR to add it to classpath, because I'm in a different directory, if not you can cd to that dir containing your Main.jar
and then invoke your Main class like this
请注意,我已经提到了 JAR 的绝对路径以将其添加到类路径中,因为我在不同的目录中,如果没有,您可以 cd 到包含您的目录Main.jar
,然后像这样调用您的 Main 类
C:\Project>java -classpath Main.jar mainPkg.Main
Here Main.jar
is inside Project directory so no need to give absolute path
这Main.jar
是在项目目录中,因此无需提供绝对路径