Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Executing batch files stored in Jar (http://www.velocityreviews.com/forums/t550912-executing-batch-files-stored-in-jar.html)

Lionel van den Berg 11-11-2007 10:11 AM

Executing batch files stored in Jar
 
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?= 11-11-2007 02:29 PM

Re: Executing batch files stored in Jar
 
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

Roedy Green 11-11-2007 06:14 PM

Re: Executing batch files stored in Jar
 
On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
<lionelv_@gmail.com> 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

Lionel van den Berg 11-12-2007 08:05 AM

Re: Executing batch files stored in Jar
 
Roedy Green wrote:
> On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
> <lionelv_@gmail.com> 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.

Sabine Dinis Blochberger 11-12-2007 10:13 AM

Re: Executing batch files stored in Jar
 
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

=?UTF-8?B?QXJuZSBWYWpow7hq?= 11-12-2007 01:59 PM

Re: Executing batch files stored in Jar
 
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

Gordon Beaton 11-12-2007 02:27 PM

Re: Executing batch files stored in Jar
 
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

--

zfkmk@yahoo.com 11-12-2007 07:20 PM

Re: Executing batch files stored in Jar
 
On Nov 12, 7:59 am, Arne Vajh°j <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 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


=?ISO-8859-1?Q?Arne_Vajh=F8j?= 11-13-2007 02:43 AM

Re: Executing batch files stored in Jar
 
zfkmk@yahoo.com wrote:
> On Nov 12, 7:59 am, Arne Vajh°j <a...@vajhoej.dk> 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

Eugene Zharkov 11-13-2007 03:16 AM

Re: Executing batch files stored in Jar
 
On Nov 12, 8:43 pm, Arne Vajh°j <a...@vajhoej.dk> wrote:
> zf...@yahoo.com wrote:
> > On Nov 12, 7:59 am, Arne Vajh°j <a...@vajhoej.dk> 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



All times are GMT. The time now is 09:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.