The Artima Developer Community
Sponsored Link

Java Answers Forum
calling unix script from a java program

7 replies on 1 page. Most recent reply: Jun 22, 2002 3:30 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Sourabh

Posts: 1
Nickname: chats
Registered: Jun, 2002

calling unix script from a java program Posted: Jun 21, 2002 10:33 AM
Reply to this message Reply
Advertisement
i am using :
Process myProc = Runtime.getRuntime().exec("/home/sourabh/testscript");

this runs without an error, but does not execute anything in the testscript even a simple echo statement. I am running on HP unix platform).

Any pointers ??

chats


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: calling unix script from a java program Posted: Jun 21, 2002 7:03 PM
Reply to this message Reply
Plz. search the forum, this problem has been discussed before.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 21, 2002 9:30 PM
Reply to this message Reply


/* ProcessRunner.java
* @author: Charles Bell
* @version: June 21, 2001
*/

import java.io.*;

/** Executes the String arguments, displays the commands being executed, and displays
* any results.
*/
public class ProcessRunner{

/** Instantiates a class and runs the commands passed in the aguments.
*/
public static void main(String[] args){
ProcessRunner runner = new ProcessRunner();
String[] command = {"/home/sourabh/testscript"};
runner.runCommand(command);
}

/** Runs the commands in a process and displays the results.
*/
public void runCommand(String[] args){
int number = args.length;
try{
String[] commands;


if (System.getProperty("os.name").compareToIgnoreCase("windows") > 0){
commands = new String[number + 2];
commands[0] = "command.com"; //for Windows 95, 98, ME. etc.
if (System.getProperty("os.name").compareToIgnoreCase("nt") > 0){
commands[0] = "cmd.exe"; //for Windows NT
}
commands[1] = "/c";
for (int i = 0; i < number;i++){
commands[i+2] = args[i];
}
}else{
commands = new String[number];
for (int i = 0; i < number;i++){
commands[i] = args[i];
}
}


System.out.print("Executing: ");
for (int i = 0; i< commands.length;i++){
System.out.print(commands[i] + " ");
}


Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(commands);
// Because some native platforms only provide limited buffer size
// for standard input and output streams, failure to promptly write
// the input stream or read the output stream of the subprocess
// may cause the subprocess to block, and even deadlock.
CheckStream csin = new CheckStream(process.getInputStream());
CheckStream cserr = new CheckStream(process.getErrorStream());
csin.start();
cserr.start();
System.out.print("Waiting for command process to terminate.");
int done = process.waitFor();
process.destroy();
System.out.println("... Done.");
}catch(InterruptedException ie){
System.out.println("InterruptedException: " + ie.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}


/** Inner class for checking the results if any of an InputStream.
*/
class CheckStream extends Thread{
BufferedReader br;
String lineread = "";
/** Constructor needs an InputStream to form an anonymous
* InputStreamReader which is used to create a BufferedReader
* for reading the stream.
*/
CheckStream(InputStream is){
this.br = new BufferedReader(new InputStreamReader(is));
}


/** Reads the input stream and displays anything returned.
*/
public void run(){
try{
while ((lineread = br.readLine()) != null){
System.out.println(lineread);
}
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 21, 2002 10:53 PM
Reply to this message Reply
Hey Charles,

Looks like you are building up your auto-answer list. :-)

If you want to add Windows XP support to the list, the OS name will be "Windows XP" and the command shell is "cmd.exe" (like NT).

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 22, 2002 10:59 AM
Reply to this message Reply
Yes it is really sad. I'll add the windows XP thing to this little canned utility. The whole premise of my program is bad java programming, because it involves operating specific stuff. I am sort of ashamed to author it.

It seems that 90% of the questions posted to all these forums is either how to make an applet do local file manipulations which its not supposed to do, to interface to microsoft word, to jury rig java to do operating specific things that java is not designed to do, or for some lame old man like me to do their homework assigments. I think I have posted a version of the above code in response to dozens and dozens of similar requests. In just a week or two I will invariably get another request just like the previous one but to do another operating specific thing. But as a full time instructor, I am very used to this. (I reach operators how to operate a nuclear power plant!).

I think we need to start a new forum topic for jni stuff. Matt, I noticed in one of your posts that you were pretty good at it. When you step back and think about it, there is a real demand for all things c, c++, and java and how to get all programs to interact.
I have been toying around with the Jacob open source stuff. Perhaps we could get something going to make something useful from it.

I honestly do not want to turn any body off here. I just keep on trying to answer a question if I can and leave the stuff I have no clue about alone.

Programming is enjoyable for me. Even all the simple homework assignments. It keeps my mind active and invariably I learn something valuable each and every time. Even more importantly it helps me deal with the mental depression I suffer from. So if anyone actually read down this far, its okay with me to keep your requests coming in. You can email me personally at: charbell@bellsouth.net I can only do what I can. I can not get to every thing and I am not the master of everything. All I ever wanted was a simple thanks if I actually did something of value for someone.

Best wishes to everyone!!

Charles

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 22, 2002 11:50 AM
Reply to this message Reply
Hey Charles,

I don't think JNI stuff is necessarily a bad thing; it is simply realistic that people will want to do things on their platform that they know are possible. For instance, the tray icons in Windows are pretty popular, so people will want to use them. I think it is nice to have a program which conditionally does things to take advantage of its environment -- the problems only arise when the program stupidly tries to use operating system-specific features without regard to the OS upon which it is currently running.

I found two projects called Jacob on SourceForge, but neither one seems to be the to which you refer. Where is the Jacob you are talking about? Is it a framework for facilitating platform-specific feature usage?

I think another nice thing to do is have a top-ten FAQ (maybe with a few categories, like Applets, JNI, etc.) where we post simple but complete and documented example programs, like the one you've posted in this thread and the others you've posted as well as some I and a few others have posted. Then, instead of always having to post all the code each time, we can say "See item 3 in the JNI FAQ at http://www.IncrediblyNiftyJavaCodingExamples.com." I've nagged Bill several times about having a link to such a thing right on the main page of this Java Forum, but we both are pretty busy (or he doesn't like the idea and doesn't want to hurt my tender feelings by saying so!), so it hasn't yet happened.

I know there are FAQs all over the web, but obviously people are not finding them if they are always posting the questions in the forum. So a forum with an accompanying FAQ could be a nice solution. I agree with you that people should not always be castigated for posting a question that has already been answered or that they could easily find in the Java docs, Java Tutorials, or Google ...well, maybe there are a few egregious cases where some excoriation is appropriate! ;-)

I also agree that it can be fun to solve some of the problems posted, in particular when it gives you a chance to delve into something you may not otherwise have occasion to do. Of course, my enthusiasm for such things is invariably inversely proportional to the amount of my real work load!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 22, 2002 12:07 PM
Reply to this message Reply
By the way, a nice feature of this FAQ would be to have a facility for downloading the java file, so the formatting is not all lost as it is in the process of copying and pasting from a web page.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: calling unix script from a java program Posted: Jun 22, 2002 3:30 PM
Reply to this message Reply
Agreed

Flat View: This topic has 7 replies on 1 page
Topic: How to use the functions in class Graphics to draw in JTextArea Previous Topic   Next Topic Topic: Byte Manipulation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use