How to call DOS cmd?

W

wang

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
 
G

Gordon Beaton

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
 
?

.

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.
 
A

Alun Harford

wang said:
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.
 
R

Roedy Green

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
 
W

wang

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.
 
R

Ravi

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.
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top