I created a JAR file like this:
jar cf Predit.jar *.*
I ran this JAR file by double clicking on it (it didn\'t work). So I ran it from the
Eclipse Runnable JAR File
Create a Java Project – RunnableJAR
Main Class
public class RunnableMainClass {
public static void main(String[] args) throws InterruptedException {
System.out.println("Name : "+args[0]);
System.out.println(" ID : "+args[1]);
}
}
Run Jar file using java program (cmd) by supplying arguments and get the output and display in eclipse console.
public class RunJar {
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
String jarfile = "D:\\JarLocation\\myRunnable.jar";
String name = "Yash";
String id = "777";
try { // jarname arguments has to be saperated by spaces
Process process = Runtime.getRuntime().exec("cmd.exe start /C java -jar "+jarfile+" "+name+" "+id);
//.exec("cmd.exe /C start dir java -jar "+jarfile+" "+name+" "+id+" dir");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream ()));
String line = null;
while ((line = br.readLine()) != null){
sb.append(line).append("\n");
}
System.out.println("Console OUTPUT : \n"+sb.toString());
process.destroy();
}catch (Exception e){
System.err.println(e.getMessage());
}
}
}
In Eclipse to find Short cuts:
Help ► Help Contents ► Java development user guide ► References ► Menus and Actions
You have to add a manifest to the jar, which tells the java runtime what the main class is. Create a file 'Manifest.mf' with the following content:
Manifest-Version: 1.0
Main-Class: your.programs.MainClass
Change 'your.programs.MainClass' to your actual main class. Now put the file into the Jar-file, in a subfolder named 'META-INF'. You can use any ZIP-utility for that.
java -classpath Predit.jar your.package.name.MainClass
If you don`t want to create a manifest just to run the jar file, you can reference the main-class directly from the command line when you run the jar file.
java -jar Predit.jar -classpath your.package.name.Test
This sets the which main-class to run in the jar file.
To run jar, first u have to create
executable jar
then
java -jar xyz.jar
command will work