Executing batch files stored in Jar

Discussion in 'Java' started by Lionel van den Berg, Nov 11, 2007.

  1. I have some batch files stored in a jar file. Can I execute them while
    they are in the jar file or do I need to get them out first and put them
    in a temporary directory?

    I'm aware of how to load them: getClass().getResource("/batchfile.bat")
    but where do I go from there?

    Thanks

    Lionel.
    Lionel van den Berg, Nov 11, 2007
    #1
    1. Advertising

  2. Lionel van den Berg wrote:
    > I have some batch files stored in a jar file. Can I execute them while
    > they are in the jar file or do I need to get them out first and put them
    > in a temporary directory?
    >
    > I'm aware of how to load them: getClass().getResource("/batchfile.bat")
    > but where do I go from there?


    You need to write it out.

    BAT files are executed by cmd.exe and cmd.exe are not jar aware.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 11, 2007
    #2
    1. Advertising

  3. Lionel van den Berg

    Roedy Green Guest

    On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I have some batch files stored in a jar file. Can I execute them while
    >they are in the jar file or do I need to get them out first and put them
    >in a temporary directory?


    Windows has no concept of Jars. You will have to unpack them then exec
    them. See http://mindprod.com/jgloss/exec.html
    http://mindprod.com/jgloss/zip.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 11, 2007
    #3
  4. Roedy Green wrote:
    > On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >> I have some batch files stored in a jar file. Can I execute them while
    >> they are in the jar file or do I need to get them out first and put them
    >> in a temporary directory?

    >
    > Windows has no concept of Jars. You will have to unpack them then exec
    > them. See http://mindprod.com/jgloss/exec.html
    > http://mindprod.com/jgloss/zip.html


    Thanks to both replies. I was hoping that I was wrong but you have
    confirmed the contrary.
    Lionel van den Berg, Nov 12, 2007
    #4
  5. Arne Vajhøj wrote:

    > Lionel van den Berg wrote:
    > > I have some batch files stored in a jar file. Can I execute them while
    > > they are in the jar file or do I need to get them out first and put them
    > > in a temporary directory?
    > >
    > > I'm aware of how to load them: getClass().getResource("/batchfile.bat")
    > > but where do I go from there?

    >
    > You need to write it out.
    >
    > BAT files are executed by cmd.exe and cmd.exe are not jar aware.
    >
    > Arne


    I wonder if you could pipe it in? Just a curiousity.
    --
    Sabine Dinis Blochberger

    Op3racional
    www.op3racional.eu
    Sabine Dinis Blochberger, Nov 12, 2007
    #5
  6. Sabine Dinis Blochberger wrote:
    > Arne Vajhřj wrote:
    >> Lionel van den Berg wrote:
    >>> I have some batch files stored in a jar file. Can I execute them while
    >>> they are in the jar file or do I need to get them out first and put them
    >>> in a temporary directory?
    >>>
    >>> I'm aware of how to load them: getClass().getResource("/batchfile.bat")
    >>> but where do I go from there?

    >> You need to write it out.
    >>
    >> BAT files are executed by cmd.exe and cmd.exe are not jar aware.

    >
    > I wonder if you could pipe it in? Just a curiousity.


    You mean:

    program-that-read-from-jar-and-write-to-stdout | cmd

    ?

    Interesting idea.

    I don't know if it will work.

    Arne
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Nov 12, 2007
    #6
  7. On Mon, 12 Nov 2007 08:59:00 -0500, Arne Vajhøj wrote:
    > Sabine Dinis Blochberger wrote:
    >> I wonder if you could pipe it in? Just a curiousity.

    >
    > You mean:
    >
    > program-that-read-from-jar-and-write-to-stdout | cmd
    >
    > ?
    >
    > Interesting idea.
    >
    > I don't know if it will work.


    While the pipe example may just work, realize that in this case cmd
    isn't executing a batch file per se, it's executing a sequence of
    commands entered as though they were entered individually at the
    command line.

    IIRC batch files can contain some things that work differently (or
    perhaps not at all) when entered on the command line, although I admit
    that things may have changed in the 15 or so years since I've used MS
    DOS. Testing the value of environment variables comes to mind.

    The suggestion would however likely work with any Unix shell, which
    doesn't typically make this kind of distinction.

    /gordon

    --
    Gordon Beaton, Nov 12, 2007
    #7
  8. Lionel van den Berg

    Guest

    On Nov 12, 7:59 am, Arne Vajhøj <> wrote:
    > Sabine Dinis Blochberger wrote:
    > > Arne Vajh j wrote:
    > >> Lionel van den Berg wrote:
    > >>> I have some batch files stored in a jar file. Can I execute them while
    > >>> they are in the jar file or do I need to get them out first and put them
    > >>> in a temporary directory?

    >
    > >>> I'm aware of how to load them: getClass().getResource("/batchfile.bat")
    > >>> but where do I go from there?
    > >> You need to write it out.

    >
    > >> BAT files are executed by cmd.exe and cmd.exe are not jar aware.

    >
    > > I wonder if you could pipe it in? Just a curiousity.

    >
    > You mean:
    >
    > program-that-read-from-jar-and-write-to-stdout | cmd
    >
    > ?
    >
    > Interesting idea.
    >
    > I don't know if it will work.
    >
    > Arne


    Why not? Well, depending on what the OP wants, there may be issues, of
    course. I just tried the following quick and dirty test and it seems
    to work:

    import java.io.*;
    import java.util.*;
    import static java.lang.System.out;

    public class Bat {
    public static void main (String[] args) throws Exception {
    String bat =
    "echo Hello\n" +
    "set java=%SystemRoot%\\system32\\java.exe\n" +
    "set | find \"java\"\n" +
    "dir %java%\n";
    Runtime rt = Runtime.getRuntime ();
    Process process = rt.exec ("cmd");
    InputStream stderr = process.getErrorStream ();
    InputStream stdout = process.getInputStream ();
    OutputStream stdin = process.getOutputStream ();
    stdin.write (bat.getBytes ());
    stdin.close ();
    BufferedReader br;
    String line;
    br = new BufferedReader (new InputStreamReader (stdout));
    while ((line = br.readLine ()) != null)
    out.println ("stdout=" + line);
    br = new BufferedReader (new InputStreamReader (stderr));
    while ((line = br.readLine ()) != null)
    out.println ("stderr=" + line);
    }
    }

    Eugene
    , Nov 12, 2007
    #8
  9. wrote:
    > On Nov 12, 7:59 am, Arne Vajhøj <> wrote:
    >> You mean:
    >>
    >> program-that-read-from-jar-and-write-to-stdout | cmd
    >>
    >> ?
    >>
    >> Interesting idea.
    >>
    >> I don't know if it will work.

    >
    > Why not? Well, depending on what the OP wants, there may be issues, of
    > course. I just tried the following quick and dirty test and it seems
    > to work:
    >
    > import java.io.*;
    > import java.util.*;
    > import static java.lang.System.out;
    >
    > public class Bat {
    > public static void main (String[] args) throws Exception {
    > String bat =
    > "echo Hello\n" +
    > "set java=%SystemRoot%\\system32\\java.exe\n" +
    > "set | find \"java\"\n" +
    > "dir %java%\n";
    > Runtime rt = Runtime.getRuntime ();
    > Process process = rt.exec ("cmd");
    > InputStream stderr = process.getErrorStream ();
    > InputStream stdout = process.getInputStream ();
    > OutputStream stdin = process.getOutputStream ();
    > stdin.write (bat.getBytes ());
    > stdin.close ();
    > BufferedReader br;
    > String line;
    > br = new BufferedReader (new InputStreamReader (stdout));
    > while ((line = br.readLine ()) != null)
    > out.println ("stdout=" + line);
    > br = new BufferedReader (new InputStreamReader (stderr));
    > while ((line = br.readLine ()) != null)
    > out.println ("stderr=" + line);
    > }
    > }


    Yep.

    But try with:

    String bat =
    "@echo off\n" +
    "goto bot\n" +
    ":top\n" +
    "echo top\n" +
    "goto fin\n" +
    ":bot\n" +
    "echo bot\n" +
    "goto top\n" +
    ":fin\n";

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 13, 2007
    #9
  10. On Nov 12, 8:43 pm, Arne Vajhøj <> wrote:
    > wrote:
    > > On Nov 12, 7:59 am, Arne Vajhøj <> wrote:
    > >> You mean:

    >
    > >> program-that-read-from-jar-and-write-to-stdout | cmd

    >
    > >> ?

    >
    > >> Interesting idea.

    >
    > >> I don't know if it will work.

    >
    > > Why not? Well, depending on what the OP wants, there may be issues, of
    > > course. I just tried the following quick and dirty test and it seems
    > > to work:

    >
    > > import java.io.*;
    > > import java.util.*;
    > > import static java.lang.System.out;

    >
    > > public class Bat {
    > > public static void main (String[] args) throws Exception {
    > > String bat =
    > > "echo Hello\n" +
    > > "set java=%SystemRoot%\\system32\\java.exe\n" +
    > > "set | find \"java\"\n" +
    > > "dir %java%\n";
    > > Runtime rt = Runtime.getRuntime ();
    > > Process process = rt.exec ("cmd");
    > > InputStream stderr = process.getErrorStream ();
    > > InputStream stdout = process.getInputStream ();
    > > OutputStream stdin = process.getOutputStream ();
    > > stdin.write (bat.getBytes ());
    > > stdin.close ();
    > > BufferedReader br;
    > > String line;
    > > br = new BufferedReader (new InputStreamReader (stdout));
    > > while ((line = br.readLine ()) != null)
    > > out.println ("stdout=" + line);
    > > br = new BufferedReader (new InputStreamReader (stderr));
    > > while ((line = br.readLine ()) != null)
    > > out.println ("stderr=" + line);
    > > }
    > > }

    >
    > Yep.
    >
    > But try with:
    >
    > String bat =
    > "@echo off\n" +
    > "goto bot\n" +
    > ":top\n" +
    > "echo top\n" +
    > "goto fin\n" +
    > ":bot\n" +
    > "echo bot\n" +
    > "goto top\n" +
    > ":fin\n";
    >
    > Arne


    Oh well. I guess it is not that simple, after all.

    Eugene
    Eugene Zharkov, Nov 13, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric McIntyre

    Jar files in Jar files

    Eric McIntyre, May 24, 2004, in forum: Java
    Replies:
    1
    Views:
    472
    Roedy Green
    May 24, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    564
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,714
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,748
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    653
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page