Reputation: 6618
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]#
Upvotes: 0
Views: 5061
Reputation: 51721
The error
Error: Could not find or load main class spi_qa.jar
clearly indicates that java
treated your spi_qa.jar
argument as the name of the class that contains your main()
method (a class named jar inside an spi _ qa package because of the dot notation). This happened because the syntax you've used is used to execute a Java.class
file.
To execute a jar
use
java -cp . -jar spi_qa.jar
Since, you've exported your Jar using Eclipse, Manifest.mf
details must have been taken care of by the IDE. If you still run into errors, check that your Manifest contains the Main-class
attribute like
Main-class: spi_qa.Program
To execute your class explicitly (independent of manifest.mf entries, or in its absence)
java -cp spi_qa.jar spi_qa.Program
Upvotes: 1
Reputation: 1967
Command to run a jar is java -jar jarFile.
This should help you run the jar file provided it is packaged properly with Main-Class being set in the Manifest file.
Edit: other options to the java command still remain the same, like cp for classpath.
Also, just to make sure that you are running this file as root, have you tried running this from the terminal, with sudo? I think this should actually help you run it as root. I am not really sure, but if it works fine you can actually avoid all this jar file creation just to run this program.
Upvotes: 0
Reputation: 333
You can add your main class in the jar manifest and can execute the jar directly.
Upvotes: 0