0

I've seen this question asked a lot, and I did check the other ones, and none of the responses seem to match my issue.

I'm new to Java so I probably did something stupid. I'm using Eclipse Juno Service Release 2 on CentOS 6.4 and OpenJDK 1.7.

I created a Java Project, then a package "spi_qa" under /src. Then I created a class called Program.java and another one TestCaseConfiguration.java. I want the entry point of the program to be Program. This is (a simplified version of) the code:

package spi_qa;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import au.com.bytecode.opencsv.*;

public class Program {
    static String configDirectory = "/spi/share/QA/conf";
    static String csvFile = "/spi/share/QA/csv/testcases.csv";

    public static void main(String[] args) {
        System.out.println("- Starting QA Test Suite -");
        // Some QA stuff
        System.out.println("- QA Test Suite completed -");
    }

I have to run this as root (don't ask), and I can't do that from within Eclipse, so I went ahead to export the program through File>Export>Java>JAR file, selected my "spi_qa" package and all the files in there, and picked spi_qa.Program as the Main class. This exports fine. However when I run it, I get this:

[root@localhost Downloads]# java -cp . spi_qa.jar 
Error: Could not find or load main class spi_qa.jar
[root@localhost Downloads]# 

And I have absolutely no clue how that can be.

I checked and I have the corresponding:

[root@localhost Downloads]# java -version
java version "1.7.0_19"
OpenJDK Runtime Environment (rhel-2.3.9.1.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
[root@localhost Downloads]# 
4

3 回答 3

1

错误

Error: Could not find or load main class spi_qa.jar

清楚地表明java将您的spi_qa.jar参数视为包含您的方法的类的名称(由于点表示法,在 spi _ qa 包中名为jarmain()发生这种情况是因为您使用的语法用于执行文件。Java.class

执行jar使用

java -cp . -jar spi_qa.jar

由于您已经使用 Eclipse 导出了 Jar,Manifest.mf因此 IDE 必须处理好细节。如果您仍然遇到错误,请检查您的清单是否包含Main-class类似的属性

Main-class: spi_qa.Program

显式执行你的类(独立于 manifest.mf 条目,或者在它不存在的情况下)

java -cp spi_qa.jar spi_qa.Program
于 2013-06-18T15:37:41.717 回答
0

运行 jar 的命令是java -jar jarFile

这应该可以帮助您运行 jar 文件,前提是它已正确打包,并且在 Manifest 文件中设置了 Main-Class。

编辑:java 命令的其他选项仍然保持不变,例如类路径的 cp。

另外,为了确保您以 root 身份运行此文件,您是否尝试过使用 sudo 从终端运行此文件?我认为这实际上应该可以帮助您以 root 身份运行它。我不太确定,但如果它工作正常,你实际上可以避免创建所有这些 jar 文件,只是为了运行这个程序。

于 2013-06-18T15:36:51.900 回答
0

您可以在 jar 清单中添加主类,并可以直接执行 jar。

于 2013-06-18T15:37:41.163 回答