简体   繁体   中英

.jar doesn't work when exported (file path)

I am trying to merge 2 simple programs. I want to make into one .jar file and this is the code I have written: (project1.jar and project2.exe are both packaged into this .jar)

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*/
      }
   }
}

Everything works just fine when I run the project in eclipse (both files are executed, like I want). But when I export this project to .jar and run it nothing happens. I think it has something to do with file paths since it works fine when run in eclipse. How to fix this?

If you're packaging both of those jars into a third jar, this won't work.
For your "cmd..." to see and use those two .jar files, they must exist directly on the disk rather than be packaged inside another jar file.

To solve this, you could:
- distribute the 3 jar files, and use your "main" class as above. You may have to tweak "main" to get the jar paths right.
or
- unpack the classes from those two jars and jar their .class files into your big jar, then change your "main" to class the correct entrypoint classes rather than referencing .jar files.
or
- use onejar as someone else has mentioned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM