How to call DOS cmd?

Discussion in 'Java' started by wang, Oct 12, 2005.

  1. wang

    wang Guest

    Hi all,
    I'd like to call a batch file from a java program. I think I should
    find the way in JNI. But after I've read the java tutorial for JNI,
    it seems to me that one can only call a method in a .dll file, and that
    there is no simple way as in C for calling a system cmd:

    system("dir /w /on");

    How can effect as above be achieve in java in a simple way? A simple
    example would be very helpful. Thanks in advance!

    k.w.wang
     
    wang, Oct 12, 2005
    #1
    1. Advertising

  2. On 12 Oct 2005 07:36:11 -0700, wang wrote:
    > I'd like to call a batch file from a java program. I think I should
    > find the way in JNI. But after I've read the java tutorial for JNI,
    > it seems to me that one can only call a method in a .dll file, and
    > that there is no simple way as in C for calling a system cmd:
    >
    > system("dir /w /on");
    >
    > How can effect as above be achieve in java in a simple way? A simple
    > example would be very helpful. Thanks in advance!


    While you cannot call system() directly from Java, you can call a JNI
    method in a DLL that calls system() or any other C or system functions
    you like.

    As it happens, the special case of system() has already been
    considered by the designers of the language. Have a look at
    Runtime.exec().

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 12, 2005
    #2
    1. Advertising

  3. wang

    wang Guest

    Oh, this is the java equivalent for system(). Thank you very much,
    Gordon!
     
    wang, Oct 12, 2005
    #3
  4. wang

    . Guest

    On Wed, 12 Oct 2005, wang wrote:

    > Hi all,
    > I'd like to call a batch file from a java program. I think I should
    > find the way in JNI. But after I've read the java tutorial for JNI,
    > it seems to me that one can only call a method in a .dll file, and that
    > there is no simple way as in C for calling a system cmd:
    >
    > system("dir /w /on");
    >
    > How can effect as above be achieve in java in a simple way? A simple
    > example would be very helpful. Thanks in advance!


    Have a look at java.lang.Runtime. This class has a number of different
    exec() methods. One should do what you want.

    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
     
    ., Oct 12, 2005
    #4
  5. wang

    Alun Harford Guest

    "wang" <> wrote in message
    news:...
    > Oh, this is the java equivalent for system(). Thank you very much,
    > Gordon!
    >


    Not quite, but nearly.
    In C, system() never fails. In java, Runtime.exec() can fail because of the
    security policy, so you need to be far more careful using it.

    --
    Include the word 'lemongrass' in any email you send to me, or you'll hit my
    spam filter. If you're reading archives, I may have changed this word -
    check http://www.alunharford.co.uk/
     
    Alun Harford, Oct 12, 2005
    #5
  6. wang

    Roedy Green Guest

    On 12 Oct 2005 07:36:11 -0700, "wang" <> wrote or
    quoted :

    >I'd like to call a batch file from a java program. I think I should
    >find the way in JNI. But after I've read the java tutorial for JNI,
    >it seems to me that one can only call a method in a .dll file, and that
    >there is no simple way as in C for calling a system cmd:
    >
    >system("dir /w /on");


    see http://mindprod.com/jgloss/exec.html

    You must fire up a command processor and pass in your dir command as
    an argument.

    You can do a dir command much more easily inside java with File.list
    and a FilenameFilter.

    See http://mindprod.com/jgloss/file.html
    http://mindprod.com/jgloss/filter.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 12, 2005
    #6
  7. wang

    wang Guest

    Thank you all who have posted answer to my question to help me.
    I have just taken "dir" as an example to explain what I want to
    do on DOS level. Actually I want to use Ghostscript to convert
    a PostScript file into a PDF file by invoking a batch file
    "ps2pdf14.bat",
    which should save me much time to re-invent the wheel in java - if I'm
    able to achieve that at all.
    Thank you all again.
     
    wang, Oct 13, 2005
    #7
  8. wang

    Ravi Guest

    Hi,
    The Below Code will ease your work....but make sure that the path of
    ..bat(batch file is specified in exec method....


    class MyRun
    {
    public static void main(String args[])
    {
    Runtime rt=Runtime.getRuntime();
    try
    {
    rt.exec("cmd /c ps2pdf14.bat");
    }catch(Exception e){e.printStackTrace();}
    }
    }

    Regards,
    Ravindra Kumar.
     
    Ravi, Oct 14, 2005
    #8
  9. wang

    Roedy Green Guest

    On 13 Oct 2005 08:50:42 -0700, "wang" <> wrote or
    quoted :

    >a PostScript file into a PDF file by invoking a batch file
    >"ps2pdf14.bat",
    >which should save me much time to re-invent the wheel in java - if I'm
    >able to achieve that at all.
    >Thank you all again.

    same thing. You spawn a command processor with the bat file as
    parameter.

    You can experiment in a dos box to get the form of the command and
    parameter correct.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 15, 2005
    #9
  10. wang

    wang Guest

    Hi all,
    I've found the solution. Thank you very much!
    k.w.wang
     
    wang, Oct 16, 2005
    #10
  11. wang

    cutietoy

    Joined:
    Nov 25, 2008
    Messages:
    1
    So what was the solution you found?
     
    cutietoy, Nov 25, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,115
    Peter Otten
    Jun 3, 2005
  2. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    390
    Bengt Richter
    Jun 26, 2005
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    406
    Scott David Daniels
    Mar 8, 2006
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    570
    Diez B. Roggisch
    May 29, 2008
  5. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    354
    Michele Simionato
    May 29, 2008
Loading...

Share This Page