Re: Executing batch files stored in Jar
On Nov 12, 7:59 am, Arne Vajh=F8j <a...@vajhoej.dk> 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 t=
hem
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