using jave to run linux and windows commands

P

Pat

hi all
I would like to write a gui to run windows and linux commands, is this
possable ?

the aim would be to write a GUI that would run on top of the os and for
example, I would like to run dir on a windows box i.e. I would click
dir on the windows box, or else on linux run ls on the Linux box ?

would this be possable and what library's would you use to do this ?

Thanks for you help in advance
Pat Rice
 
A

ahbrandt

Pat said:
hi all
I would like to write a gui to run windows and linux commands, is this
possable ?

the aim would be to write a GUI that would run on top of the os and for
example, I would like to run dir on a windows box i.e. I would click
dir on the windows box, or else on linux run ls on the Linux box ?

would this be possable and what library's would you use to do this ?

Thanks for you help in advance
Pat Rice

Its a bit cumbersome. The dir command is not a "dir.exe" program but
instead a buildin command of the command prompt. Therefore you have to
some extra tricks to get it working. Here is a complete example that
works and can be used for running any type of commands. On unix you
don't need the temp file ie:

String[] params = {"df", "-k", "/" + path};
Process p = Runtime.getRuntime().exec(params);

will do the trick.


import java.io.*;

public class Test {

public static void main(String[] args) {
File script = new File(System.getProperty("java.io.tmpdir"),
"script.bat");

PrintWriter writer = null;
InputStream reader = null;
try {
writer = new PrintWriter(new FileWriter(script, false));
writer.println("dir");
writer.close();
// get the output from running the .bat file
Process p =
Runtime.getRuntime().exec(script.getAbsolutePath());
reader = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = reader.read();
if (c == -1) {
break;
}
buffer.append((char) c);
}
String outputText = buffer.toString();
reader.close();

System.out.println("outputText = " + outputText);

} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// do nothing
}
}
}
}
}

regards

Anders Holmbech Brandt
 
I

Ian Wilson

Multiposting is a horribly irritating thing to do! Are you aware of the
difference between multiposting and crossposting?

See http://www.cs.tut.fi/~jkorpela/usenet/xpost.html

Hello Pat.
I would like to write a gui to run windows and linux commands, is this
possable ?

(s/possable/possible/; s/gui/GUI/; s/windows/Windows/; s/linux/Linux/.)

Yes it is possible.
the aim would be to write a GUI that would run on top of the os

(s/os/O\/S/.)

That's good, it's many orders of magnitude harder to write a GUI that
doesn't run on the O/S.
and for
example, I would like to run dir on a windows box i.e. I would click
dir on the windows box, or else on linux run ls on the Linux box ?

You could phrase this better, but I think I know what you mean. You'd
have a button labelled "dir", when this button is clicked, the program
would execute the corresponding command for whatever operating system is
running the program.
would this be possable

(s/possable/possible/.)

You already asked this. Yes it is possible.
and what library's would you use to do this ?

(Note: the plural of library is libraries. Also the apostrophe is never
used to form plurals. See http://www.apostrophe.fsnet.co.uk/)

I would do it without using any special libraries. For cross-platform
GUI apps I'd use Swing. System.getProperty("os.name") might come in
handy. I'd Google for "java external program" to find
http://www.rgagnon.com/javadetails/java-0014.html

Unless you are writing some kind of GUI command-shell program, I suspect
you'd be better off using cross-platform Java methods. As advised by
someone else in one of the other newsgroups you copied this message to.

P.S. I'm only this fussy about punctuation and spelling when irritated,
see top of posting for cause of irritation.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top