Run .exe packaged in .jar

2019-07-28 02:35发布

问题:

I am trying to merge 2 programs I have made to one .jar file. One program is a .jar written in java and the second one is an .exe written in c++. I put both files to the new .jar, wrote this code but it didn't work. When this code was exported to .jar and executed neither of 2 files ran and I got error "no main manifest attribute, in merged.jar" in cmd. Though it worked perfectly when run in eclipse.

  public class main
    {
        public static void main(String[] args) 
   {
      try 
   {
      Runtime.getRuntime().exec("cmd /c project1.jar");
      Runtime.getRuntime().exec("cmd /c project2.exe");
   }  
   catch(Exception exce)
     { 
     /*handle exception*/
      }
       }
   }

Any idea how to fix this or is there another way to do it? I am new to java, so can't think of anything good. Maybe it would be possible to drop these files to a temporary location in windows and delete them after they're executed?

回答1:

Have a look at the JAR File Specification.

You have to update your MANIFEST file to populate a "Main-Class" attribute with the class that contains you main() method.



回答2:

You can try this:

    String filePath = "C:/Path/to/my/file.exe";
    try {

        Process p = Runtime.getRuntime().exec(filePath);

    } catch (Exception e) {
        e.printStackTrace();
    }