1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    You cannot run an exe that is inside a jar file. Windows won't find it there. – Thilo Jun 21 '12 at 08:52
  • What does the EXE do? What is the advantage of putting a Java app. as wrapper to the EXE? Why not write that part in C++ as well? – Andrew Thompson Jun 21 '12 at 08:54
  • General tips. 1) Use a consistent and logical indent for code blocks. 2) In every `catch`, add `e.printStackTrace()` as part of it. 3) Copy/paste the error output and use code formatting. 4) In the event that `/*handle exception*/` is not **literally** the code being used in the exception handler, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 21 '12 at 09:01

2 Answers2

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.

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Arcadien
  • 2,258
  • 16
  • 26
1

You can try this:

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

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

    } catch (Exception e) {
        e.printStackTrace();
    }
Zakk
  • 21
  • 1
  • I would say '+1 for `printStackTrace()`' but for: 1) This resource (the executable) is inside a Jar. So the path stated will not point to it. No file path can. **It is *not* a file.** 2) Use `ProcessBuilder` to construct the `Process`. That makes it at least one step easier to: 3) Implement **all** the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html) – Andrew Thompson Jun 21 '12 at 09:09
  • does not answer the question, the .exe is packaged in the jar, so the path of the exe is related to the path of the jar. – Enrico Giurin Apr 19 '20 at 07:05