简体   繁体   English

尝试运行jar文件但无法找到类

[英]Trying to run jar file but getting cannot find class

I am trying to run a jar file from the Win7 command line, but am getting the dreaded could not find or load main class PRCreateExecution. 我试图从Win7命令行运行一个jar文件,但是却无法找到或加载主类PRCreateExecution。

I can successfully build the jar file from a Win7 batch file on the command line. 我可以在命令行上从Win7批处理文件成功构建jar文件。

My current manifest file is named PRCreateExecution.mf and is located in here: C:\\WDEclipseIDEWorkspace\\MC3\\src\\PurchaseRequests\\ 我当前的清单文件名为PRCreateExecution.mf,位于以下位置:C:\\ WDEclipseIDEWorkspace \\ MC3 \\ src \\ PurchaseRequests \\

The manifest file contains: 清单文件包含:

Manifest-Version: 1.0
Created-By: 1.8.0_40 (Oracle Corporation)
Main-Class: PurchaseRequests.PRCreateExecution.class

(extra LF is here) (额外的LF在这里)

I run the Win7 batch file to build the jar from C:\\WDEclipseIDEWorkspace\\MC3\\src\\PurchaseRequests: 我运行Win7批处理文件以从C:\\ WDEclipseIDEWorkspace \\ MC3 \\ src \\ PurchaseRequests构建jar:

jar -cvmf PRCreateExecution.jar C:\WDEclipseIDEWorkspace\MC3\bin\PurchaseRequests\PRCreateExecution.mf C:\WDEclipseIDEWorkspace\MC3\bin\PurchaseRequests\PRCreateExecution.class C:\WDJarFiles

The jar file gets created successfully. jar文件创建成功。

Now I'm using this batch statement to try and run the jar file: 现在,我使用此批处理语句尝试运行jar文件:

java -cp C:\WDEclipseIDEWorkspace\MC3\bin\PurchaseRequests;. PurchaseRequests.PRCreateExecution

from in here: 从这里:

C:\WDEclipseIDEWorkspace\MC3\src\PurchaseRequests

but am getting the could not find main class PurchaseRequests.PRCreateExecution. 但正在获取找不到主类PurchaseRequests.PRCreateExecution。

PRCreateExecution source snippet: PRCreateExecution源代码片段:

package PurchaseRequests;
public class PRCreateExecution {
public static void main(String[] args)

Thanks for any help... 谢谢你的帮助...

Firstly, it sounds like you're not actually trying to run your jar file at all. 首先,听起来您实际上根本没有在尝试运行jar文件。 You're not mentioning it anywhere on your command line. 您不会在命令行的任何地方提及它。

Secondly, it looks like your classpath is wrong - it should probably be 其次,看来您的类路径是错误的-可能应该是

java -cp C:\WDEclipseIDEWorkspace\MC3\bin;. PurchaseRequests.PRCreateExecution

That's assuming that the bin directory contains a PurchaseRequests directory which contains PRCreateExecution.class 假设bin目录包含一个PurchaseRequests目录,其中包含PRCreateExecution.class

Thirdly, you should follow Java naming conventions for packages - they should be lower case. 第三,您应该遵循Java软件包的命名约定-它们应为小写。

I faced a similar kind of issue building distributable jar files using Netbeans. 我遇到了使用Netbeans构建可分发jar文件的类似问题。 My suggestion would be to try and run the jar directly from the command line and from within the directory path of its location. 我的建议是尝试直接从命令行以及在其位置的目录路径内运行jar。

It is seen that you have something like this in your manifest file: 可以看到清单文件中包含以下内容:

Manifest-Version: 1.0 Created-By: 1.8.0_40 (Oracle Corporation) Main-Class: PurchaseRequests.PRCreateExecution.class (extra LF is here)

I suggest you change the lines as follows: 我建议您将行更改如下:

Manifest-Version: 1.0 Created-By: 1.8.0_40 (Oracle Corporation) (insert CF)
Main-Class: PurchaseRequests.PRCreateExecution.class (extra LF is here)

You may use this as your reference: 您可以以此为参考:

https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

This post might be of use, too: 这篇文章也可能有用:

http://stackoverflow.com/questions/12767886/use-of-manifest-file-in-java

Remove the .class suffix from the manifest. 从清单中删除.class后缀。

It should look like: 它应该看起来像:

Manifest-Version: 1.0
Created-By: 1.8.0_40 (Oracle Corporation)
Main-Class: PurchaseRequests.PRCreateExecution

Afterwards run java -jar (name of your jar-file).jar 然后运行java -jar (name of your jar-file).jar

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

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