The Artima Developer Community
Sponsored Link

Java Answers Forum
IOException using runtime.exec

3 replies on 1 page. Most recent reply: May 5, 2002 4:39 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 3 replies on 1 page
daniel kirk

Posts: 1
Nickname: kirky
Registered: May, 2002

IOException using runtime.exec Posted: May 3, 2002 8:57 AM
Reply to this message Reply
Advertisement
Hi there,

I have an issue related to trying to run a process through Runtime.exec.

An IOException is thrown with the message IOException: curl not found (the command is curl).

Typically if a program cannot be found through the path, the error message is "error 2" NOT "not found".

This program works on my home NT Box running Resin, but does not work on the production unix server running JRun. Once upon a time it did work on the production server, but I don't know what could have changed to make it stop working. The webmasters have little idea but do not think it's a permissions problem.

Has anyone experienced this before or have a suggestion on how to fix it?

thank you

Daniel Kirk
editor@toptipper.com


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: IOException using runtime.exec Posted: May 3, 2002 3:42 PM
Reply to this message Reply
Daniel,

Quickly throwing your question's Key word on google I got this : http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Though there is some Jazz onthe radio I going to hit the sack (it's 12:45 AM).

Hope to see what the issue is & it's solution on the forum !

Tx for the annoying question :-D

Thomas SMETS,
SCJP 2 - Brussels


p.s. :
This was my query :
http://www.google.com/search?sourceid=navclient&querytime=IGQ&q=%22error+2%22++Runtime%2Eexec+java

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: IOException using runtime.exec Posted: May 5, 2002 3:12 PM
Reply to this message Reply
Your problem may be how you are running your command.
This program can be easily revised for Unix or Linux if needed. I no longer have a Unix or Linux machine to test it on. Check this out and see if anything in this source will help. I may have some other examples if I hunt around.


/* RuntimeDemo.java
* @author: Charles Bell
* @version: January 6, 2001
* Example use of Runtime.
*/

import java.io.*;
import java.lang.*;

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

/** Instantiates a class and runs the commands passed in the aguments.
*/
public static void main(String[] args){
RuntimeDemo demo = new RuntimeDemo();
demo.runCommand(args);
}


/** Runs the commands in a process and displays the results.
*/
public void runCommand(String[] args){
int number = args.length;
try{
String[] commands;
String operatingsystem = System.getProperty("os.name");
if (operatingsystem.toLowerCase().indexOf("windows") > 0){
commands = new String[number + 2];
commands[0] = "command.com"; //for Windows 95, 98, ME. etc.
if (operatingsystem.toLowerCase().indexOf("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());
}
}
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: IOException using runtime.exec Posted: May 5, 2002 4:39 PM
Reply to this message Reply
Also something may have happened with a new release of Java. Are you using any deprecated methods?

Suggest you log into the developer connection at java.sun.com and look for bugs or post one on your particular problem.

cURL is a Unix program. You can get the latest download at:
http://curl.haxx.se/download.html

Flat View: This topic has 3 replies on 1 page
Topic: JTree Previous Topic   Next Topic Topic: Array List

Sponsored Links



Google
  Web Artima.com   

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