How to run a JAR file

前端 未结 11 2281
长发绾君心
长发绾君心 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

提交回复
热议问题