5 Replies Latest reply: May 9, 2008 4:51 PM by 807601 RSS

    Executing a file from within a .jar File?

    807601
      Hey my names milk long time reader first time poster :P

      i was just wondering if anyone could tell me how to execute a program from within a .jar file?
      (unfortunatly dont have the code on me not on my TAFE (like uni) drive)

      Ill explain as best i can

      i have a GUI program and one of the buttons if ActionPerformed will execute a .bat file,

      it works in the folder that i created it in
      E.G

      lets say i compile the jar file in C:\program, and i packaged it all together Main.class, filetoexe.bat

      but if i move the jar file to C:\tryhere if ActionPerformed it does nothing

      does that make sense?


      all im trying to find out, is if it is posible to execute the file from within the jar

      ill post my code tonight, that might help more


      Thanks heaps in advance
      -MilkMang
        • 1. Re: Executing a file from within a .jar File?
          807601
          ActionPerformed has nothing to do with this.

          I guess your question is how to make a self-executing jar. See [http://java.sun.com/docs/books/tutorial/deployment/jar/]
          • 2. Re: Executing a file from within a .jar File?
            807601
            The main part of an executable .jar file is a file called "manifest.txt" All you need to do is create a TXT file called manifest.txt and in the file place this text:
            Main-Class: PracticeGui
            Where is says "PracticeGui" replace with the name of your class that hold your Main method. Then package this manifest.txt inside the .jar file along with your classes. This way the JVM will be able to find the "main" method and execute the program.
            • 3. Re: Executing a file from within a .jar File?
              807601
              Thanks for the tutorial cotton.m

              and im aware of the manifest.txt the jar file works and executes

              the only thing is once i run the program on any other computer once i click the button(within the program) it does not execute the batch file


              __________
              |_________ | <---- Click here button executes watever.bat (this works on my comp)

              ------------------------------------------------------------------------------------------------------------------
              __________
              |_________ | <---- Click here button does NOT execute watever.bat (on any other computer)


              does that make sense?

              and this is the contents of my jar file

              jar cmf manifest.txt MyApp.jar Sphere.class sphere.bat

              Edited by: MilkMang on May 8, 2008 8:28 PM
              • 4. Re: Executing a file from within a .jar File?
                807601
                MilkMang wrote:
                Thanks for the tutorial cotton.m

                and im aware of the manifest.txt the jar file works and executes

                the only thing is once i run the program on any other computer once i click the button(within the program) it does not execute the batch file


                __________
                |_________ | <---- Click here button is suppose to execute watever.bat (this works on my comp)

                ------------------------------------------------------------------------------------------------------------------
                __________
                |_________ | <---- Click here button does NOT execute watever.bat (on any other computer)


                does that make sense?

                and this is the contents of my jar file

                jar cmf manifest.txt MyApp.jar Sphere.class sphere.bat
                Ah I see.

                So what you have is in your Java program you want to launch a batch file using Runtime.exec and that isn't working?

                Well a Jar file is like a zip file. It has files inside of it but outside of Java if you want the OS to be able to see them then they have to be extracted. There's an excellent tutorial/article on compression and decompression of Zip files in Java available here [http://java.sun.com/developer/technicalArticles/Programming/compression/]. The steps to unzip a Jar file are pretty much the same (and in fact if I am not crazy you can in fact treat a Jar file as if it was a zip file) as the code that's included there.
                • 5. Re: Executing a file from within a .jar File?
                  807601
                  Thanks Hepas guys,

                  really appreciate it thats pretty much what im trying to do,

                  now just to implement it into my code :)