Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Executing batch files stored in Jar

Reply
Thread Tools

Executing batch files stored in Jar

 
 
Lionel van den Berg
Guest
Posts: n/a
 
      11-11-2007
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.
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      11-11-2007
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-11-2007
On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
<(E-Mail Removed)> 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
 
Reply With Quote
 
Lionel van den Berg
Guest
Posts: n/a
 
      11-12-2007
Roedy Green wrote:
> On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Sabine Dinis Blochberger
Guest
Posts: n/a
 
      11-12-2007
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
 
Reply With Quote
 
=?UTF-8?B?QXJuZSBWYWpow7hq?=
Guest
Posts: n/a
 
      11-12-2007
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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      11-12-2007
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

--
 
Reply With Quote
 
zfkmk@yahoo.com
Guest
Posts: n/a
 
      11-12-2007
On Nov 12, 7:59 am, Arne Vajh°j <(E-Mail Removed)> 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

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      11-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Nov 12, 7:59 am, Arne Vajh°j <(E-Mail Removed)> 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
 
Reply With Quote
 
Eugene Zharkov
Guest
Posts: n/a
 
      11-13-2007
On Nov 12, 8:43 pm, Arne Vajh°j <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Nov 12, 7:59 am, Arne Vajh°j <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java -cp a.jar -jar b.jar => Works on Windows, not on Debian cyberco Java 4 02-14-2006 06:27 AM
jaas.jar, jta.jar jdbc-stdext.jar missing from jdk1.5 RPM muttley Java 0 10-20-2005 02:40 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters Java 0 01-05-2005 10:59 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters XML 0 01-05-2005 10:59 PM
Jar files in Jar files Eric McIntyre Java 1 05-24-2004 07:24 PM



Advertisments