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

Tip: Looking for answers? Try searching our database.

executing unix programs through java

Thread view: 
ruds - 19 Aug 2006 11:27 GMT
hello,
I want to know how to execute unix based application programs through
java/JSP/servlet?
is it possible to do system level programming through java like in C?
PerfectDayToChaseTornados - 19 Aug 2006 17:10 GMT
> hello,
> I want to know how to execute unix based application programs through
> java/JSP/servlet?
> is it possible to do system level programming through java like in C?

You can run a shell on the comand line using Runtime.exec()

String[] cmdArray = new String[3];
       cmdArray[0] = "/usr/local/bin/bash";
       cmdArray[1] = "-c";
       cmdArray[2] = "pwd"; // your command

try {
           // start command running
           Process proc = Runtime.getRuntime().exec(cmdArray);

           // get command's output stream and
           // put a buffered reader input stream on it
           InputStream istr = proc.getInputStream();
           BufferedReader br = new BufferedReader(new
InputStreamReader(istr));
           try {

               // read output lines from command
               String str;
               while ((str = br.readLine()) != null) {
                   // process your results
               }
               // wait for command to terminate
               try {
                   proc.waitFor();
               }
               catch (InterruptedException ie) {
                   LOGGER.error("InterruptedException caught ", ie);
                   // possibly throw an exception here
               }

               // check its exit value
               if (proc.exitValue() != 0) {
                   InputStream errorStream = proc.getErrorStream();
                   BufferedReader brErr = new BufferedReader(new
InputStreamReader(errorStream));
                   String errStr;
                   StringBuffer errSB = new StringBuffer();
                   while ((errStr = brErr.readLine()) != null) {
                       errSB.append(errStr + "\n");
                   }
                   LOGGER.error("exit value was non-zero : \n" +
errSB.toString());
                  // possibly throw an exception here
               }
           }
           finally {
               br.close();
           }
       }
       catch (Exception ex) {
           // handle exception
       }

Hope this helps :-)

Signature

pdtct

ruds - 20 Aug 2006 07:17 GMT
>                     LOGGER.error("InterruptedException caught ", ie);
>                     LOGGER.error("exit value was non-zero : \n" +

I didnt understand these two lines
and it gives error Cannot resolve symbol for it.
Do i have to import any class for it?
PerfectDayToChaseTornados - 20 Aug 2006 17:42 GMT
>>                     LOGGER.error("InterruptedException caught ", ie);
>>                     LOGGER.error("exit value was non-zero : \n" +
>
> I didnt understand these two lines
> and it gives error Cannot resolve symbol for it.
> Do i have to import any class for it?

This was just example code & I was using log4j for logging. You can use
whatever you like for logging. The code I gave you doesn't really do
anything it is an example which you need to understand if you want to use
it.

Signature

pdtct

ruds - 21 Aug 2006 07:28 GMT
thanks a lot pdtct!!


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.