You can always use JNI to call native programs. It's a bit of work
though.
-Cameron McKenzie
www.scja.com www.pulpjava.com www.examscam.com
> hi all
> I would like to write a gui to run windows and linux commands, is this
[quoted text clipped - 8 lines]
> Thanks for you help in advance
> Pat Rice
> hi all
> I would like to write a gui to run windows and linux commands, is this
[quoted text clipped - 8 lines]
> 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
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
> hi all
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.