Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Executing batch files stored in Jar

Thread view: 
Lionel van den Berg - 11 Nov 2007 10:11 GMT
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.
Arne Vajhøj - 11 Nov 2007 14:29 GMT
> 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
Sabine Dinis Blochberger - 12 Nov 2007 10:13 GMT
Arne Vajhřj 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
[quoted text clipped - 8 lines]
>
> Arne

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

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

Arne Vajhøj - 12 Nov 2007 13:59 GMT
> Arne Vajhřj wrote:
>>> I have some batch files stored in a jar file. Can I execute them while
[quoted text clipped - 8 lines]
>
> 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 - 12 Nov 2007 14:27 GMT
>> I wonder if you could pipe it in? Just a curiousity.
>
[quoted text clipped - 7 lines]
>
> 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 - 12 Nov 2007 19:20 GMT
On Nov 12, 7:59 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > Arne Vajh j wrote:
> >>> I have some batch files stored in a jar file. Can I execute them while
[quoted text clipped - 20 lines]
>
> 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
Arne Vajhøj - 13 Nov 2007 02:43 GMT
>> You mean:
>>
[quoted text clipped - 38 lines]
>   }
> }

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 - 13 Nov 2007 03:16 GMT
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:
[quoted text clipped - 57 lines]
>
> Arne

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

Eugene
Roedy Green - 11 Nov 2007 18:14 GMT
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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lionel van den Berg - 12 Nov 2007 08:05 GMT
> On Sun, 11 Nov 2007 20:11:26 +1000, Lionel van den Berg
> <lionelv_@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 7 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.