How to run a JAR file

前端 未结 11 2144
长发绾君心
长发绾君心 2020-11-22 04:18

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

相关标签:
11条回答
  • 2020-11-22 04:48

    Eclipse Runnable JAR File

    Create a Java Project – RunnableJAR

    • If any jar files are used then add them to project build path.
    • Select the class having main() while creating Runnable Jar file.

    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

    0 讨论(0)
  • 2020-11-22 04:51

    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.

    0 讨论(0)
  • 2020-11-22 04:58
    java -classpath Predit.jar your.package.name.MainClass
    
    0 讨论(0)
  • 2020-11-22 04:59

    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.

    0 讨论(0)
  • 2020-11-22 04:59

    To run jar, first u have to create

    executable jar

    then

    java -jar xyz.jar

    command will work

    0 讨论(0)
提交回复
热议问题