Process prcs2 = rt.exec("cmd.exe /c \"e:\\filename\"");  How in UNIX?

I have this current process running from a servlet on a NT server. It is kicking off an excel macro which creates a graph in excel, and then displaying the graph. I want to kick off a command in UNIX from a different servlet. What would I have to use? If anyone knows, please email me at [email protected]
Justin Mennen

I would like to run a shell script on OS390 from my servlet. How would I do it? I have a similar process that works with NT listed below. What would I do to run a script on UNIX immediately from the servlet?
Process prcs2 = rt.exec("cmd.exe /c \"e:\mydirectory\filename\"");

Similar Messages

  • Problem with cmd.exe  /C SET

    Hi:
    I am trying to set environment variables for an exe file in a Java program.
    I am thinking to do:
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd.exe /C set FILEDIR=c:\temp);
    But, I tried cmd.exe /C set FILEDIR=c:\temp on my xp machine dos window and in the java code. The FILEDIR is null.
    Does someone have any idea what went wrong? Anyother better ways to set environment variables in java codes?
    Thanks.

    cmd.exe opens a new command process. The set command will only be effective in this particular process. As soon as the process closes (which will be immediately the set command completes) the value of FILEDIR will be lost.
    You can see this by doing cmd /K set FILEDIR=c:\temp. This will leave the command process open and you see what the value of FILEDIR is.

  • How to end a process from cmd.exe?

    I need to open and close a word file(Test.doc) from my java program. I managed to open the file using cmd shell, but I don't know how to close the opened file. Does anybody have a suggestion? Below is my code. Thanks.
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
        p = r.exec("cmd.exe /c start Test.doc");
        catch (Exception e) {
          System.out.println("Error opening the file.");
        }

    Try something like this:
    Runtime r = Runtime.getRuntime();
    Process p = null;
    String command = "\"C:\\Program Files\\Microsoft Office\\Office12\\winword\"";
    String file = "\"c:\\Test.doc\"";
    try {
        p = r.exec(command + " " + file);    //to open the file in MS Office Word
        Thread.sleep(30000);    //keep it open for 30secs
        p.destroy();    //close the process and hence the file
    }catch (Exception e) {
        System.out.println("Error opening the file.");
    }Edited by: Sourav-Sipani on Jul 28, 2008 1:56 AM

  • Controlling "CMD.EXE" via Process - Never ends?

    I'm trying to write a Java program that parses a text file & executes various command promt commands (I'm trying to keep it platform independant, because I know Lin/Un/nix users who would find this userful as well). I found a small tutorial on the web which gave an example of:
    Process p = Runtime.getRuntime().exec("<YOUR COMMAND HERE>");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
    Now; I have inserted "cmd.exe" for <YOUR COMMAND HERE>. If I write a command out to stdOutput, the first command executes, but not subsequent ones. Small segment looks like:
    while (line != null) {
    stdOutput.write("mkdir \"" + new_folder + "\"\n");
    stdOutput.flush();
    line = file_reader.readLine();
    This is an abridged version of my ode, but thats the problem. I get the first folder created, the process never terminates and I've tried emptying out the stdInput & stdErr bufferes, but it still won't execute subsequent commands issued to stdOutput.
    Can someone help me? I'd rather not write this all to a .BAT/.sh file :(

    I am quite perplexed now! I have just tried the following code on my PC, and have ended up with the most annoying two folders under my C:\ drive!
    import java.io.*;
    public class Cmd {
        public static void main(String[] args) {
            try {
                Process p = Runtime.getRuntime().exec("cmd.exe");
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
                BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
    //            System.out.println(stdInput.readLine());
    //            System.out.println(stdInput.readLine());
    //            System.out.println(stdInput.readLine());
                stdOutput.write("mkdir \"c:\\folder1\"\n");
                stdOutput.flush();
    //            System.out.println(stdInput.readLine());
                stdOutput.write("mkdir \"c:\\folder2\"\n");
                stdOutput.flush();
    //            System.out.println(stdInput.readLine());
            } catch (IOException e) {
                e.printStackTrace();
    Perlexion over! Since you had your code in a loop I tried the same. I saw that in a loop creating 10 folders, the code created only two. See below the code that fixed this problem:
    import java.io.*;
    public class Cmd {
        public static void main(String[] args) {
            try {
                Process p = Runtime.getRuntime().exec("cmd.exe");
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
                BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
                System.out.println(stdInput.readLine());
                System.out.println(stdInput.readLine());
                System.out.println(stdInput.readLine());
                stdOutput.write("mkdir \"c:\\folder1\"\n");
                stdOutput.flush();
                System.out.println(stdInput.readLine());
                stdOutput.write("mkdir \"c:\\folder2\"\n");
                stdOutput.flush();
                System.out.println(stdInput.readLine());
            } catch (IOException e) {
                e.printStackTrace();
    }Commenting out different combinations of System.outs gives different results.
    In summary: it is important that you match the correct number of output lines from the CMD.EXE output stream with readLines.
    I didn't like how the wording came out, but you get the gist.
    HTH,
    Manuel Amago.

  • Runtime.exec and cmd.exe

    Just a quick one here, I think. I'm working on a little tool to merge changes into a document and then copy it over a communications port, and everything seems to be checking out except for that last step. Rather than worrying about streams and whatnot, I'm saving the file to the hard drive; from here, my plan has been to open the DOS prompt and take advantage of its "copy (filename) (port)" command to transfer the file. So in the end, the key line of Java should be something like
    Runtime.getRuntime().exec("cmd.exe copy \"" + fileName + "\" LPT1");My issue here is that the command prompt doesn't seem to want to open using Runtime.exec(). It seems like cmd.exe should do the job -- tossing that into the run menu certainly opens the prompt -- and even just to be sure, I've tried using C:\WINDOWS\system32\cmd.exe explicitly, and both that at cmd.exe without any additional arguments. Strangely, though, the prompt isn't opening, and I'm not getting an I/O error out of it as if I was sending in a bogus command.
    So what am I doing wrong here? What do I have to do to open the DOS prompt?

    As I mentioned, I did try providing the full path to cmd.exe, to no effect.
    Curiously, though, changing "cmd.exe" into "cmd.exe /k" or "cmd.exe /c" both cuased the command to run correctly. Contrary to what they're supposed to do, though, both of them result in the immediate termination of the DOS prompt, even though /k is supposed to cause the window to persist. Any ideas as far as that one goes?

  • Where to put files in using runtime.exec(run.exe)?

    I try to use the following program in EJB to call my EXE program in the J2EE server:
    runtime.exec("c:\\j2ee\\public\\exe\\run.exe temp.txt");
    In the execution of mill_turn.exe, it will open several files for read and write.
    So where to put the file temp.txt and other files the run.exe need to read?
    Looks like the J2EE server can not find them?
    Thanks!

    I got it in Java SE1.4.
    File dir=new File("c:\\j2ee\\public_html\\Mill_turn\\exe\\");
    Process proc = rt.exec(cmd,null,dir);Use above codes, I really got the right output from my external program for the first time. But , after that, the output keeps same when I change the filename in the following code:
    cmd="c:\\..\\run.exe temp.prt"
    to
    cmd="c:\\..\\run.exe Abc1.prt" , which must have different output.
    The codes for calling external program are implemented in the SessionBean of J2EE.

  • Interacting with cmd.exe

    Hi all,
    i dunno if this has been asked before, but my problem is to let the user interact with an executable ran via "cmd.exe /c executable".
    i have an executable program that awaits a response from the user, but when i used the Streams provided by java.lang.Process after many scenarios i noticed that the process inputStream provides me with the output only when the program is finished, while it returns no text while the program is waiting for response
    as an example, my problem looks like :
    try {
                Process p = Runtime.getRuntime().exec("cmd.exe /c mysql -u toto");
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                  String s = null;
                while ((s = reader.readLine()) != null) {
                        System.out.println(s);
    } catch (IOException ex) {
                ex.printStackTrace();
    }the mysql command would show "enter password : " or something like that, but this program will not show it...unless the mysql is finished
    any ideas? thanks :)

    that's right, i tried to look for classes that provide "live streams" in java.io ... i thought the PipedReader and PipedWriter could do it, but i need to get the program's reader and writer for this (and i don't know how to do that...) or maybe those classes are unnecessary and there is another way to do it?

  • Process proc=runtime.exec("sh","-c","//home//usr//mkdir abcd") not working

    my servlet calling the above line is not executing to make a directory
    "abcd" at specified location. i am using Mandrake Linux and Tomcat... can anybody expalin the error.

    Hi raghutv,
    I also have this error
    I use Tomcat 4.0 and Window Me
    Do u think that the problem is "Window OS" or Tomcat Setting
    I have a problem. I want to complie the other Java Program "hi.java" using servlet.
    I am compiling the servlet code succesfully, but the web page can't display anything.
    The real path of "hi.java" is in the
    D:\Program Files\Apache Tomcat 4.0\FYP\WEB-INF\classes\hi.java
    This is my servlet code..............pls help.............thx!
    /*********************** Hello.java *********************/
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Hello extends HttpServlet
    public void service(HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException
    try {
    String path = getServletContext().getRealPath("\\WEB-INF\\classes\\hi.java");
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("javac.exe " + path);
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    PrintWriter com_out = res.getWriter();
    com_out.println("<pre>");
    String line = null;
    while((line = br.readLine()) != null){
    com_out.println(line);
    //String[] cmd = { "ping.exe", "time.nist.gov" }; // Command and argument
    //String[] cmd = { "javac.exe","hi.java"}; // Command and argument
    //Process process = Runtime.getRuntime().exec(cmd); // Execute command
    //Process process = Runtime.exec(cmd); // Execute command
    //process.waitFor(); // Wait until process has terminated
    catch (Exception e) {
    System.err.println(e);

  • 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.

  • Question about cmd.exe /k

    I can write the code below to start cmd.exe to stay and not end:
    Process p=Runtime.getRuntime().exec("cmd.exe /K ver"); // for example.
    InputStream in=p.getInputStream();
    InputStream err=p.getErrorStream();Now how do I send another command to this process p and capture its output?
    OutputStream o=p.getOutputStream();Can someone help? I just want to run one copy of cmd.exe.

    google for the many discussions about this.

  • Writing to  cmd.exe

    Hello!
    Want to write a program where i can write to a dos promt already started. Example code below
    Process run = Runtime.getRuntime().exec("cmd.exe");
    InputStream s = run.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(s));
    String read = new String();
    System.out.print(br.readLine());
    OutputStream o = run.getOutputStream();
    //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(o));
    //bw.write("md test");
    //bw.flush();
    //bw.newLine();
    o.write("md test".getBytes());
    o.flush();
    o.close();
    Problem with this code is that i cant write to to the dos promt, but i can read from it! what is wrong? The program itself isnt very logic, i know i could send the md test command directly with hte exec function. The main idea is to be able to send a command to an already running dospromt, in essence, start the program, depending on output from command, exectue another appropriate command.
    Examples and refrences to java doc is appreciated
    Best
    Dan

    The other poster has the right idea. You code is right, but you need to have it in a threaded environment.
    Also, when you go to write out to the cmd line do something like this:
    bw.write("md test \r\n");
    bw.flush();The "\r\n" is the carriage return line feed expected by dos. Having the bw.newLine in there apears not to do anything (tried it myself).
    Below is a quick and dirty example of a program that runs, and does what you are looking for. (I think)
    * Dos.java
    * Created on October 6, 2003, 3:07 PM
    import java.io.*;
    public class Dos {
        /** Creates a new instance of Dos */
        public Dos() {
            try{
                Process run = Runtime.getRuntime().exec("cmd.exe");
                new ReadMe(run).start();
                new WriteMe(run).start();
            }catch(Exception e){
                e.printStackTrace();
         * @param args the command line arguments
        public static void main(String[] args) {
            new Dos();
    class WriteMe extends Thread{
        Process run;
        public WriteMe(Process run){
            this.run = run;
        public void run(){
            try{
                InputStream s = run.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(s));
                String read = new String();
                while((read = br.readLine()) != null){
                    System.out.println(read);
            }catch(IOException ioe){
    class ReadMe extends Thread{
        Process run;
        public ReadMe(Process run){
            this.run = run;
        public void run(){
            try{
                OutputStream o = run.getOutputStream();
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(o));
                bw.write("cd \\ \r\n dir c: \r\n");
                bw.flush();
            }catch(IOException ioe){
    }Hope this helps.

  • Exec(iexplore.exe) works in IE6 but not IE7

    The following statement invoked from an applet works fine with IE6 but not with IE7:
    Process process = Runtime runtime.exec(iexplore.exe slicprint.html))
    With IE6 the browser (iexplore.exe) is launched and the slicprint.html file is rendered (appears on screen).
    With IE7 the program just "hangs", i.e.the browser does not get launched (nothing displays on the screen).

    Welcome to the Sun forums. But..
    >
    The following statement invoked from an applet works fine with IE6 but not with IE7:>
    Process process = Runtime runtime.exec(iexplore.exe slicprint.html))..please refrain from posting such rot. That statement could not compile, let alone run.
    Instead, it is better to [copy/paste errors and code snippets,|http://pscode.org/javafaq.html#exact] or better still, post an SSCCE of failing code.
    Hmm.. and just so we are all clear. Why exactly are you not calling [AppletContext.showDocument(URL)|http://java.sun.com/javase/6/docs/api/java/applet/AppletContext.html#showDocument(java.net.URL)] to achieve this functionality that has been available to sandboxed applets since Java 1.1 and works, most of the time, cross-browser and cross-platform?

  • Spawn a java process using runtime.exec() method

    Hi,
    This is my first post in this forum. I have a small problem. I am trying to spawn a java process using Runtime.getRuntime().exec() method in Solaris. However, there is no result in this check the follwoing program.
    /* Program Starts here */
    import java.io.*;
    public class Test {
    public static void main(String args[]) {
    String cmd[] = {"java", "-version"};
    Runtime runtime = Runtime.getRuntime();
    try{
    Process proc = runtime.exec(cmd);
    }catch(Exception ioException){
    ioException.printStackTrace();
    /* Program ends here */
    There is neither any exception nor any result.
    The result I am expecting is it should print the following:
    java version "1.4.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
    Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
    Please help me out in this regard
    Thanks in advance
    Chotu.

    Yes your right. It is proc.getInputStream() or proc.getErrorStream(). That is what I get for trying to use my memory instead of looking it up. Though hopefully the OP would have seen the return type of the other methods and figured it out.

  • Cmd.exe /c

    To run an external program from java in a windows environment, i am using Runtime.exec("cmd.exe /c myfile.bat").
    I have two questions to ask:
    1. I want to know what is the significance of "cmd.exe /c" and
    why/when they needs to be used. What do they actually mean.
    2. What is the equivalent of "cmd.exe /c" for unix/solaris
    environment.
    Regards,
    Nischal

    Hi,
    .bat was an example for the windows environment. For the unix/solaris environemnt i have got a .sh file.
    The solution i am looking for, is a replacement for "cmd.exe /c" for a unix/solaris environment.
    For windows i am using:
    Runtime.getRuntime().exec("cmd.exe /c myfile.bat");
    for unix/solaris what should my code look like if i have a myfile.sh file.
    Regards,
    Nischal
    For
    In addition:
    ...exec("abc pqr"); stands for: run program "abc",
    give it "pqr".
    This indicates that pqr is an argument or, if it is a
    program, it must be interpreted. So, on both MSWindows
    and UNIX/LINUX you would say:
    ...exec("java xyz"); to make the xyz.class file be
    executed as it must be interpreted by the JAVA VM.
    If you got a real binary, say /usr/bin/calculate or
    C:\calculate.exe
    you would say:
    ...exec("/usr/bin/calculate"); or
    ...exec("C:/calculate"); /* c:/ ! */
    The difference is: what interpreter am I using on that
    platform.
    There IS a sh or ksh for MSWindows. I don't know of a
    .bat interpreter
    on UNIX. Otherwise your problem would not exist.
    ;JOOP!

  • Runtime.exec() - using cp and files with spaces on unix

    I'm using the Runtime.exec method to copy files in a unix environment. This works fine until there is a file where the name has spaces in it. I've read the article on the pitfalls of using runtime and how it breaks a string on white spaces and this is what is happening. I've also found another topic that was having the same problem, but they were using /usr/bin/dos2unix. I've tried putting quotes around the filename, but it still breaks on the first space. Any suggestions on how to get around this or another way of doing this would be greatly appreciated.
    An example of the os command string is:
    /usr/bin/cp /tmp/file with space.doc /docs
    Thanks!

    Hi!
    Well I dont have any Sun machine right here to try this but in windows It works great.
    Have you tried something like this ?
    import java.io.*;
    public class OSCopy {
        public static void main(String[] args) {
            try {
                String space = " ";
                String copycmd = "E:\\cp.cmd";
                String source = "E:\\File with space.txt";
                String destination = "E:\\tmp";
                String cmd = copycmd + space + "\"" + source + "\"" + space + destination;
                System.out.println("cmd: " + cmd);
                Runtime runtime = Runtime.getRuntime();
                Process copy = runtime.exec( cmd );
                BufferedReader reader = new BufferedReader( new InputStreamReader( copy.getInputStream()) );
                String line = null;
                while( (line = reader.readLine()) != null ) {
                    System.out.println( line );
            catch (Exception e) {
                e.printStackTrace();
    cp.cmd is a simple dos copy command
    copy %1 %2
    */good luck!

Maybe you are looking for

  • Why can't I use a debit card to verify apple id?

    I am an adult parent with 3 kids and trying to set up family sharing. The process requires that I use a credit card to verify my age, but I don't use credit cards (purposefully) so I entered my bank debit card. It says I can't use a debit card, it mu

  • IPad air FaceTime & message errors

    "an error occurred during activation - try again" - both FaceTime &  iMessage - stuck at this point;  already shut down but no results

  • Error - Cannot connect to the phone

    Hi all, have had a dig around and couldn't find anything so I hoping that someone can sort me out here. Ok I have my phone connected to the pc via bluetooth. PC Suite picks up the phone but when I click on the Contacts or Messages (or anything in tha

  • Attaching a scenegraph to a branchgroup

    Hi, I've taken a program to create scenegraph from a wrml file (cybervrml97). That works fine. But what I want to do now is to "connect" this scenegraph to a J3D hierarchy that I've created by myself: branchgroup, transformgroups... Do you think I ca

  • Set Stroke Alignment from JavaScript

    Hi, I'm trying to set the stroke style for a path item I've drawn through JavaScript, however I can't find a way to set the stroke alignment for my path item. I've checked the Illustrator JavaScript reference but I can't seem to find that option in t