Executing batch files stored in Jar

  • Thread starter Lionel van den Berg
  • Start date
L

Lionel van den Berg

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Lionel 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?

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
 
S

Sabine Dinis Blochberger

Arne said:
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.
 
G

Guest

Sabine said:
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
 
G

Gordon Beaton

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

--
 
Z

zfkmk

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
E

Eugene Zharkov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top