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 / October 2006

Tip: Looking for answers? Try searching our database.

using jave to run linux and windows commands

Thread view: 
Pat - 13 Oct 2006 13:39 GMT
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
adwords@pulpjava.com - 13 Oct 2006 14:14 GMT
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
ahbrandt@gmail.com - 13 Oct 2006 14:32 GMT
> 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
Ian Wilson - 13 Oct 2006 14:49 GMT
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.


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.