Runtime.getRuntime().exec works on windows failed on linux!

Hi,
I'm trying to execute a linux command from java 1.4.2 using Runtime.getRuntime().exec(). The linux command produces output and write to a file in my home dir. The command runs interactively in the shell and also run on windows.
The command is actually a mozilla pk12util. My java code to run this pk12util works on windows, but not on linux (fedora). When I print out the command I'm trying to run in java, I can run it interactively in bash shell.
Method below is where the problem seems to be:
public void writeCert() {
// Declaring variables
System.out.println("Entering writeCert method");
String command = null;
certFile = new File(credFileName);
// building the command for either Linux or Windows
if (System.getProperty("os.name").equals("Linux")) {
command = linuxPathPrefix + File.separator + "pk12util -o "
+ credFileName + " -n \"" + nickName + "\" -d "
+ dbDirectory + " -K " + slotpw + " -w " + slotpw + " -W "
+ pk12pw;
System.out.println("System type is linux.");
System.out.println("Command is: " + command);
if (System.getProperty("os.name").indexOf("Windows") != -1) {
// command = pk12utilLoc + File.separator + "pk12util -o
// "+credFileName+" -n "\"+nickname+\"" -d \""+dbDirectory+"\" -K
// "+pk12pw+" -w "+pk12pw+" -W "+pk12pw;
command = pk12utilLoc + File.separator + "pk12util.exe -o "
+ credFileName + " -n \"" + nickName + "\" -d "
+ dbDirectory + " -K " + slotpw + " -w " + slotpw + " -W "
+ pk12pw;
System.out.println("System type is windows.");
System.out.println("Command is: " + command);
// If the system is neither Linux or Windows, throw exception
if (command == null) {
System.out.println("command equals null");
try {
throw new Exception("Can't identify OS");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
// Having built the command, running it with Runtime.exec
File f = new File(credFileName);
// setting up process, getting readers and writers
Process extraction = null;
BufferedOutputStream writeCred = null;
// executing command - note -o option does not create output
// file, or write anything to it, for Linux
try {
extraction = Runtime.getRuntime().exec(command);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
// Dealing with the the output of the command; think -o
// option in command should just write the credential but dealing with output anyway
BufferedWriter CredentialOut = null;
OutputStream line;
try {
CredentialOut = new BufferedWriter (
new OutputStreamWriter(
new FileOutputStream(credFileName)));
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
// writing out the output; currently having problems because I am trying to run
}

My error is due to the nickname "-n <nickname" parameter error. I think the problem is having a double quotes around my nickname, but if I take out the double quotes around my nickname "Jana Test's ID" won't work either because there are spaces between the String. Error below:
Command is: /usr/bin/pk12util -o jtest.p12 -n "Jana Test's Development ID" -d /home/jnguyen/.mozilla/firefox/zdpsq44v.default -K test123 -w test123 -W test123
Read from standard outputStreamjava.io.PrintStream@19821f
Read from error stream: "pk12util: find user certs from nickname failed: security library: bad database."
null
java.lang.NullPointerException
at ExtractCert.writeCert(ExtractCert.java:260)
at ExtractCert.main(ExtractCert.java:302)
Code is:
private String nickName = null;
private void setNickname(String nicknameIn) {
nickName = nicknameIn;
// building the command for either Linux or Windows
if (System.getProperty("os.name").equals("Linux")) {
command = linuxPathPrefix + File.separator + "pk12util -o " + credFileName + " -n \"" + nickName + "\" -d " + dbDirectory + " -K " + slotpw + " -w " + slotpw + " -W " + pk12pw;
System.out.println("System type is linux.");
System.out.println("Command is: " + command);
extraction = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(
new
InputStreamReader(extraction.getErrorStream()));
PrintStream p = new PrintStream(extraction.getOutputStream());
System.out.println("Read from standard outputStream" + p);

Similar Messages

  • Opening a new browser using Runtime.getRuntime().exec(cmd); under windows

    Can Runtime.getRuntime().exec(cmd); open a URL in to a new browser in wondows OS or ...can we force it to any specific browser insted on OS's default browser...
    It does open a new bowser under MAC but not under windows..
    cmd = "'rundll32 url.dll,FileProtocolHandler"+URL (windows)
    Thanks

    public class WindowsProcess
         public static void main(String[] args)
              throws Exception
              String[] cmd = new String[3];
              cmd[0] = "cmd.exe";
              cmd[1] = "/C";
              cmd[2] = "start";
              Process process = Runtime.getRuntime().exec( cmd );
              process = Runtime.getRuntime().exec( cmd );
    }When you run the above code you should get two open dos windows.
    Now just just enter some html filename and hit enter in each window. IE should start up separately in each window.
    Thats also what should happen if you specify the html file as the fourth parameter. A new process should be created and two separate windows should open with a browser in each.

  • Need help with Runtime.getRuntime().exec

    If there is a better place to post please let me know. I need some help understanding what is going on and was hoping someone might be able to provide some information. I would like to use Runtime.getRuntime().exec to copy a file in linux. Here is the code I am using:
    try {
    Process p = Runtime.getRuntime().exec("cp -v foo foo2");
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    System.out.println(line);
    } catch (IOException e) {
    e.printStackTrace();
    If I use cp, with the System.out.println(line) I can see that it has copied and of course when I check the command line indeed it has. My problem is that I really need to use the dd command, but I can't see the output that dd displays. It copies the file just fine but I need the output. If someone can provide some explaination or help I would really appreciate it. Thanks.

    By default, dd sends its output to stdout, so any status messages it has go to stderr. Use getErrorStream (double check the docs for the method name) just like you're currently using getInputSTream.

  • Runtime.getRuntime.exec() not working when Tomcat is made a windows Sevice

    Hi,
    I am working on a web application which launches a exe file on subitting the form. I am using Apache Tomcat 4.0.6 to run the web application. Initially Tomcat was not made a windows service on my machine and when I launch an exe it is launching without any problems. I used the following command to launch the exe file:
    <code>
    Process p = Runtime.getRuntime().exec("C:\\Program Files\\Mercury Interactive\\WinRunner\\arch\\wrun.exe" + strWROptions);
    </code>
    The above command launches WinRunner on the local machine.
    without using tomcat if I just compile and run a test program with this command, the exe is launching perfectly. But, Once I made tomcat a windows service on my machine the exe is not launching.
    Also, a process is being created in the windows task bar named wrun.exe once I submit the form but WinRunner is not lauching on the desktop.
    Can any one please help and suggest me.
    Thanks,
    Ramesh

    The version of Java I am using is 1.4.2. Even without
    using the string array as you mentioned, the exe
    (wrun.exe) is launched on my desktop if I don't run
    Tomcat as a windows service. But the problem arises on
    making it a service :-(
    That is strange because going by the documentation what you have been using should never work. I can't test it though, I'm not on MSWindows at the moment..
    Now, when I use this string array convention, I am
    getting an error saying "The tool could not launch..
    some error occured".
    That's not a Java error message, it must come from WinRunner. This means that you have succesfully started WinRunner (great!) but something else is wrong; maybe the parameters you pass to it are incorrect.
    How would you run it on the command line? Like this?wrun -t "D:\L5_QE\L5A\Initial" -create_text_report on -runThat line executes the wrun program and passes it five parameters, not three. The first parameter is "-t", the second "D:\L5_QE\L5A\Initial", the third "-create_text_report", the fourth "on", and the fifth "-run". The array of strings that corresponds to that line isString[] command = {"C:\\Program Files\\Mercury Interactive\\WinRunner\\arch\\wrun.exe",
                    "-t", "D:\\L5_QE\\L5A\\Initial",
                    "-create_text_report", "on",
                    "-run"};

  • Using Runtime.getRuntime().exec(DOS Program) with Windows 2000.

    I am trying to spawn a DOS Program from within Java.
    On Win95/98/ME, I use
    Process     p = Runtime.getRuntime().exec("START /w command.com con /c myDOSProgram.exe");
    This works on these platforms. On Win 2000, I use
    Process     p = Runtime.getRuntime().exec("START /w cmd.exe con /c myDOSProgram.exe");
    It does not work at all.
    I have tried all combinations of using START, cmd.exe, con /c, etc.
    I can spawn a Windows App using
    Process     p = Runtime.getRuntime().exec("windows.exe");
    And it works fine.
    What does it take to spawn a DOS program from Java on the Windows 2000 op sys?

    Hi. I recently wrote a java GUI that calls a DOS program with mixed success. Here's the code I used to make the call:String osName = System.getProperty("os.name" );
    String[] cmd = new String[4];
    if( osName.equals( "Windows 2000" ) || osName.equals( "Windows NT" ) )
         cmd[0] = "cmd.exe";
         cmd[1] = "/C" ;
         cmd[2] = "VBDoc.exe";
         cmd[3] = fileName;
    else if( osName.equals( "Windows 95" ) || osName.equals( "Windows 98" ) )
         cmd[0] = "command.com" ;
         cmd[1] = "/C" ;
         cmd[2] = "VBDoc.exe";
         cmd[3] = fileName;
    else
         System.out.println(osName);
    Process proc = runtime.exec(cmd);Check out this article, it gives a good explanation of how to use the exec() call:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    Hope this helps!

  • Problems with Runtime.getRuntime().exec in Windows 2000

    Hello,
    I have a batch file that I want to run from my java application. My code is the following:
    try {
            Runtime.getRuntime().exec("cmd.exe /c C:\\temp\\shortcut.bat");
    } catch (Exception e) {
         System.out.println(e.getMessage());
    }I was developing on windows XP and it worked just fine. But then I tested it on windows 2000 and it didn't work. The batch file is okay, because if I run the batch file myself it works just fine, even from the command line. I get no errors what so ever, it just doesn't do anything...
    Can somebody help me with this?
    thx in advance

    thank you all so much
    I figured it out... It was a combination of two things that went wrong.
    First one: in my batch file I had:
    cd C:\tempwhich worsk fine in XP, but in it doesn't in 2000. In 2000 it has to be:
    C:
    cd \temp But just changing that wasn't enough, I also needed the "start"
    Now it works just fine on 2000, hopefully it'll still work on xp as well.
    THX!

  • Runtime.getRuntime().exec("cmd /c start file\\"+"My file.txt" ) Not work

    Hello
    i have a problem I use:
    Runtime.getRuntime().exec("cmd /c start file\\"+"My file.txt");
    to open "my file.txt" And I have error System Windows don't open file file\My becaus file My don't exist When I rename file to "Myfile.txt" and use:
    Runtime.getRuntime().exec("cmd /c start file\\"+"Myfile.txt");
    It's work great. My problem is when my file have space in name or other [ ], etc symbol. How fix this problem?
    Thenks to help:)

    Try using the verson where you specify the parameters in an array as this example shows:
    http://forum.java.sun.com/thread.jspa?forumID=31&threadID=635363
    Note: you may need to enclose the file name in quotes

  • Runtime.getRuntime().exec() and waitFor on Windows

    Hi,
    I'm trying to execute a command on Windows 2000 using Runtime.getRuntime().exec(). After that I make a call to waitFor method of the returned process instance. The wait for call blocks and never releases. I'm trying to do simple things, like a call to "cmd /c dir", using commands that I know that work and do release after that. I need to wait for the command terminating to get the output just after that.
    Thanks in advance,
    Marcio Azevedo.

    It's too bad ignoring the streams couldn't be build into ProcessBuilder. Instead you could do it yourself and extend ProcessBuilder like:
    public class MyProcessBuilder extends ProcessBuilder {
      private boolean ignoreInputStream;
      public  MyProcessBuilder(List<String> command) {
         super(command);
      public  MyProcessBuilder(String... command) {
         super(command);
      public boolean ignoreInputStream() {
        return ignoreInputStream;
      public  ProcessBuilder ignoreInputStream(boolean ignoreInputStream) {
        this.ignoreInputStream= ignoreInputStream;
        return this;
      public Process start() {
        Process process = super.start();
        if (ignoreInputStream()) {
             new InputStreamIgnorer(process.getInputStream());
        return process;
      static class InputStreamIgnorer implements Runnable {
        java.io.InputStream in;
        InputStreamIgnorer(java.io.InputStream in) {
          this.in = in;
          new Thread(this,"InputStreamIgnorer Thread").start();
        public void run() {
          int ch;
          try {
            while((ch = in.read()) != -1) {
                //System.out.print(ch);
          } catch (java.io.IOException ex) {} //ignore
    }Then it could be simpled used like:
    new MyProcessBuilder("cmd","/c", "dir").redirectErrorStream(true).ignoreInputStream(true).start().waitFor();

  • How to use Runtime.getRuntime().exec() in JSP? is it works in JSP or not

    Hi to all,
    i want run a .exe file from JSP file. In java i am able do this, using Runtime.getRuntime().exec().
    but same thing, when i trying with JSP it is not working?
    plz let me is there any other ways to do it..

    It depends, usually (ie in an J2EE container) you're not allowed to access files or the runtime environment, by definition. What do you wan't to achieve with the exe?
    --olaf                                                                                                                                                                                                                                                                                                                                                           

  • SWT window disappear while running Runtime.getRuntime().exec()

    Hello,
    I have create a SWT application. I am executing the folloiwng code from my SWT application.
    Process process = Runtime.getRuntime().exec(param);
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    During execution of the above code when click on other icon of the window task bar and click back on SWT window Icon, The windows open but it does not show annything until the code execution finish. any help would be apprecited.

    This isn't an SWT forum. In the future, please post such questions on a forum specifically for SWT issues.
    But to answer your question, there's a 99.9% chance this is because you're launching the process on SWT's UI thread. Similar to performing long-running tasks on Swing's EDT, this is a no-no. The SWT UI won't be able to repaint itself until after your process has finished. To remedy this, launch your process in a separate Thread.

  • Runtime.getRuntime().exec(cmd) not working

    I have created binary scrip to parse file from one format to another format and the file parser is available on Linux path and working fine when calling from command prompt but while calling this program from jsf or any any java form bean it adds some special charaters with program and throws IOException as belows
    calling command
    String[]     cmd={"delimiter",inputFile.toString(),outputFile.toString()};
    Process     p=Runtime.getRuntime().exec(cmd);
    it gives following error.
    java.io.IOException: delimiter@! : not found
    while calling this from jaav main application working fine.
    any suggestion would be appriciated.
    Cheers,
    dharmendra

    Thanks.
    I printed out whatever values are being passed and it is printing the right values on console and in log file and I also tried this command in stand alone application program an working fine. but when I call it from jsf form bean it adds ^@ at the end of first parameter and says ;
    IOException : delimiter^@ : not found
    i actually created the same copy as delimiter^@ and put it in same path directory with 777 mode.
    and now its working fine. but I did not know the reason why it change the name of first argument.
    Thanks,

  • Runtime.getRuntime().exec doesn't work

    I write in my program this line:
    p=Runtime.getRuntime().exec("/usr/bin/identify -verbose /tmp/cible.jpg > /tmp/cible.jpg.txt");
    But the file cible.jpg.txt is not created!
    Can you explain me?
    advance thanks!
    Best regards!!!

    It doesn't work because it is literally passing the "> /tmp..." characters as a command-line argument to the "/usr/bin/identify" program, as opposed to having the shell interpreter (which you're not invoking) interpret that to mean "redirect the output to this file".

  • Don't work Runtime.getRuntime().exec

    Hi. I've written a simple programm, but it don't work(sorry for my english:)):
    import java.io.*;
    class MsgFormat {
         static String hostname;
         static String message;
    public class jPopUp {
         public static void send(MsgFormat msg) {
              if(msg.message.length()!=0) {
                   if(msg.hostname.length()!=0) {
                        try {
                             Process proc = Runtime.getRuntime().exec("echo " + msg.message
                                                                                                                                      + " | smbclient -M " + msg.hostname);
                             } catch (java.io.IOException e) {
         public static void main(String[] args) {
              try
                   MsgFormat mess = new MsgFormat();
                   System.out.println("Enter hostname: ");
                   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                   mess.hostname = in.readLine();
                   System.out.println("Enter your message: ");
                   mess.message = in.readLine();
                   send(mess);
              catch (java.io.IOException e) {}
    }

    The exec method isn't a shell. Try with something like this:String[] exec = {"/bin/bash",
                     "-c",
                     "echo "+msg.message+" | smbclient -M "+msg.hostname};
    Process proc = Runtime.getRuntime().exec(exec);

  • Runtime.getRuntime().exec() does not work after JRE upgradation

    I have upgraded JRE from 1.3.0_02 to 1.3.1_15. After this upgrade, Runtime.getRuntime().exec() has suddenly stopped working. If I downgrade to 1.3.0_02, it starts working again.
    Please help.

    It has definitely forked, and this is the child process. It says so. If it was the parent process it would just show Java code related to launching processes. That does not involve PKCS11. After the fork there are two identical processes with the same stack: the child process of the fork goes on to replace itself with the target command via Unix exec(), which it doesn't look like it is succeeding at. The parent process doesn't do much at all, probably just housekeeping and then a Unix wait() for Process.waitFor() to hook into.

  • Runtime.getRuntime.exec() not working with through Servlet

    Hi ,
    I want to open a IE from servlet,I am using a Runtime.getRuntime.exec to open the browser ,but my servlet is executing but IE is not opening...Here i am using TomcatServer...Is any settings to be Done in TomcatServer.
    My Servlet code is
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
         String s1 = "C:/Program Files/Internet Explorer/IEXPLORE.EXE";
         String s2 = "http://192.168.0.149:8080/etk/etk3.htm";
         Runtime runtime = Runtime.getRuntime();
         runtime.exec(s1 + " " + s2);
    i know it is contrary to the purpose servlets are meant for.but i am using for different purpose..
    help me on this one...
    Thanks in Advance

    No, I am using the right path, I am sure. Also I tried giving the absolute path of the UserGuide.pdf. Still it cannot find. Please anyone try spawning an application from a different directory and lemme know your openion.
    Regards,
    Thomas.

Maybe you are looking for