Problem excuteing RPM form java

P

Peter Palsherm

Hi,

I want to install RPM's (Redhat Packet Manager) out of a java
programm.
Therefore I use the getRuntime() method that allows me to execute
commands
from java.

This works fine for simple RPM's which have no pre or
postinstallationscripts.
But if I try to install a RPM-Pakete that executes a script before or
after
the installation (like webmin) the programm hangs forever.
I think when rpm trys to execute the per/postscripts it attempts to
open a new
process,fork or something like that, but the environment doesn't
support it.

Here is the java code:


public String executeRPM() {
String iStr="";
String eStr="";
StringBuffer buff= new StringBuffer();
try {
Process proc = Runtime.getRuntime().exec("rpm -iv
/path/to/rpm/webmin-1.130-1.noarch.rpm");

InputStream iStream = proc.getInputStream();
InputStream eStream = proc.getErrorStream();
BufferedReader bReader = new BufferedReader(new
InputStreamReader(iStream));
BufferedReader eReader = new BufferedReader(new
InputStreamReader(eStream));

while ((iStr = bReader.readLine()) != null)
{ buff.append(iStr);
buff.append("|");
}


buff.append("%");

while ((eStr = eReader.readLine()) != null)
{ buff.append(eStr);
buff.append("|");
}

} catch (Exception e) {
return ("Fehler in Methode executeRPM() " + e);
}
// returns errormsg and feedback from rpm
return buff.toString();
}



If I use the command : "rpm -iv --noscripts
/path/to/rpm/webmin-1.130-1.noarch.rpm"
then the installation runs without errors. But then I have a damaged /
scrap installation!

Is there a way that the environment supports forking? (Maybe a wrapper
program in c or c++? And how must it look like?)
Or can I execute the pre/postinstallstionscripts manually?

Any other solutions, workarounds or ideas????


thanks in advance

Peter Palsherm
 
G

Gordon Beaton

I want to install RPM's (Redhat Packet Manager) out of a java
programm.

Please don't multipost. Your question has been answered in another
newsgroup.

/gordon
 
P

Peter Palsherm

Thank you very much for your hints Gordon!!!

Now everything is work fine :)

Here is the complete code for People with similar problems:

/*
* OutputConsumer.java
*/

package rpmManager;

import java.io.*;

public class OutputConsumer implements Runnable{

InputStream is;
String type;
OutputStream os;
StringBuffer sb = new StringBuffer();

/** Creates a new instance of OutputConsumer */
public OutputConsumer(InputStream is, String type) {

this.is=is;
this.type=type;
}

public void run() {


try {

BufferedReader br=new BufferedReader(new
InputStreamReader(is));

String line="";

while ((line=br.readLine()) != null) {

sb.append(line);
sb.append("/n");

}
br.close();

}
catch (IOException e){
e.printStackTrace();

}
}


public StringBuffer getInputData()
{

return sb;
}


}


/*
Method that calls the OutputConsumer class
*/

public static String runRPM2 (String _cmd) {
String theResultString="";
StringBuffer eS=null;
StringBuffer oS=null;

try {
Process p = Runtime.getRuntime().exec( _cmd );

// Check if there is any Errormesage
OutputConsumer errorStream = new
OutputConsumer(p.getErrorStream(),"ERROR");

// Check if there is any Output
OutputConsumer outputStream = new
OutputConsumer(p.getInputStream(),"OUTPUT");

// Start the threads
Thread t1 = new Thread(errorStream);
t1.start();

Thread t2 = new Thread(outputStream);
t2.start();


// Check for Errors
int exitVal = p.waitFor();

theResultString.valueOf(exitVal);

eS = errorStream.getInputData();
oS = outputStream.getInputData();
}
catch(IOException ioe)
{
System.err.println("An error ocured while trying to execute
"+_cmd);
ioe.printStackTrace();
System.exit(9);
}
catch (Exception e)
{
System.err.println("An error ocured while trying to execute
"+_cmd);
e.printStackTrace();

}

theResultString=eS.toString()+" "+oS.toString();

return theResultString;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top