Runtime.exec for launching a folder.

Hi everyone,
At the end of my application I need to launch a folder with all the output files. I tried with this code:
         try
            Runtime.getRuntime().exec("cmd start " + f.getAbsolutePath());
         catch (Exception eh){
            eh.printStackTrace();
         }And it works, the problems is that the application never ends, until the user closes the launched folder.
I want to finish the application without to close the launched folder, somebody knows how to do this?
-Daniel
Edited by: zkropotkine on Feb 14, 2008 10:38 AM

I resolved the problem...

Similar Messages

  • Runtime.exec() for different Windows versions ?

    Hello all.
    I'm currently developing a Java program that uses Runtime.exec() to launch a MS-DOS application (the old MS-Kermit). It works fine under Windows ME and Java 1.3.1 but the very same code fails to work under Windows 2000. That's very bad for me, since my program should work on all windows platforms from 95 up to XP (and NT).
    My question is : can I trust Runtime.exec() or am I damned to write different versions of the same command for different operating systems ?
    Thanks.

    Runtime is not os independent, so yes, it has to accomodate the os differences. There is an enhancement request on the subject, tho.
    Search the forums, there is code that handles the differences posted.

  • Make can't recursively call Make when run from Runtime.exec (for some user)

    This one is a little complicated. The afflicted platform is Mac OS X. It works fine on Linux, it seems, and even then, it does work for some Mac OS X users...
    First, the setup. I have a Java program that has to call GNU Make. Then, GNU Make will recursively call Make in some subdirectories. This results in the following Java code:
    String make = "make"; //on windows, I have this swapped with "mingw32-make"
    String workDir = ...; //this is programmatically detected to be the same folder that the parent Makefile is in
    Runtime.getRuntime().exec(make,null,workDir);This calls make on a Makefile which has the following line early on to be executed (this is only a snippet from the makefile):
    cd subdirectory && $(MAKE)When I fetch the output from the make command, I usually get what I expect: It cd's to the directory and it recursively calls make and everything goes smoothly.
    However, for one particular user, using Mac OS X, he has reported the following output:
    cd subdirectory && make
    /bin/sh: make: command not found
    make: *** [PROJNAME] Error 127Which is like, kinda hurts my head... make can't find make, apparently.
    I've gotten some suggestions that it might be due to the "cd" command acting wonky. I've gotten other suggestions that it may be some strange setup with the env variables (My Mac developer is implementing a fix/workaround for 'environ', which is apparently posix standard, but Mac (Mr. Posix Compliance...) doesn't implement it. When he finishes that, I'll know whether it worked or not, but I get the feeling it won't fix this problem, since it's intended for another area of code entirely...
    Also worth mentioning, when the user calls "make" from the terminal in said directory, it recurses fine, getting past that particular line. (Later on down the road he hits errors with environ, which is what my aforementioned Mac dev is working on). Although calling "make" by hand is not an ideal solution here.
    Anyways, I'm looking for someone who's fairly knowledgeable with Runtime.exec() to suggest some way to work around this, or at least to find out that perhaps one of the User's settings are wonked up and they can just fix it and have this working... that'd be great too.
    -IsmAvatar

    YoungWinston
    YoungWinston wrote:
    IsmAvatar wrote:
    However, for one particular user, using Mac OS X, he has reported the following output:One particular user, or all users on Mac OS?In this case, I have two mac users. One is reporting that all works fine. The other is reporting this problem.
    cd subdirectory && make
    /bin/sh: make: command not found
    make: *** [PROJNAME] Error 127Which is like, kinda hurts my head... make can't find make, apparently.If that is being reported on the command line, then I'd say that make wasn't being found at all.If make isn't being found, then who's interpreting the Makefile?
    It's also just possible that the make script on Mac isn't correctly exporting its PATH variable, though it seems unlikely, since this would surely have been reported as a bug long ago.
    I've gotten some suggestions that it might be due to the "cd" command acting wonky...Also seems unlikely. 'cd' has been around since shortly after the K-T extinction event.
    WinstonBy "acting wonky", I mean being given a bad work directory or some such, such that it's not changing to the intended directory.
    Andrew Thompson
    Andrew Thompson wrote:
    (shudder) Read and implement all the recommendations of "When Runtime.exec() won't" (http://www.javaworld.com/jw-12-2000/jw-1229-traps.html).
    Already read it. I already know the dreadful wonders of Runtime.exec. But in this case, it's been working fine for us up until one Mac user reported that make can't find make.
    Also, why are you still coding for Java 1.4? If you are not, use a ProcessBuilder, which takes a small part of the pain out of dealing with processes.Usually I do use a ProcessBuilder. I noticed that it usually delegates to Runtime.exec() anyways, and seeing as I didn't need any of the additional functionality, I decided to just use Runtime.exec() in this particular case.
    jschell
    jschell wrote:
    So print thos env vars, in your app and when the user does it.I'll look into that. It's a good start.
    -IsmAvatar

  • Runtime.Exec for 'appref.ms' files

    Hello!
    I am using Runtime.exec to call an external application from java classes. These classes are in turn used by JSP.
    The external application is an "Application Reference" type application in Windows, and has the extension appref.ms
    From Glassfish logs, I can see the following error:
    java.io.IOException: Cannot run program ""C:\Documents and Settings\Administrator\Start Menu\Programs\ProTeleCo\QuotEncrypt.appref-ms"": CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
        at java.lang.Runtime.exec(Runtime.java:593)
        at java.lang.Runtime.exec(Runtime.java:466)
        at list.CreateNew.runExternal(CreateNew.java:251)
        at list.CreateNew.CreateQuotationDR(CreateNew.java:222)
        at list.CreateNew.processRequest(CreateNew.java:88)
        at list.CreateNew.doPost(CreateNew.java:143)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
    Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
        ... 35 moreWhen I open a command window and type
    "C:\Documents and Settings\Administrator\Start Menu\Programs\MyCompany\QuotEncrypt.appref-ms" <arg1> <arg2>
    The program runs without problems.
    I actually tried copying the QuotEncrypt.exe file that is built in the development environment to the server and give that as argument to Runtime.exec, but in that case the application seems to run (I use waitfor and the return value from waitfor) , return with 0, but it has no side effects. It does not even write to its log file.
    Hence I am at a loss to what to do.
    Appreciate any help,
    Irem

    And now the program hangs.
    It seems to be time for code indeed:
    String[] commands = {"C:\\WINDOWS\\system32\\cmd.exe", " /start ", Constants.QuotEncryptPath, " \""+qfile + "\"", Constants.encryptPassword};
      runExternal(commands);
          public static void runExternal(String commands[])
            try
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing ");
                String whole="";
                for(int i =0; i < commands.length ; i ++)
                whole = whole + commands;
    System.out.println(whole);
    Process proc = rt.exec(commands);
    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Throwable t)
    t.printStackTrace();
    import java.io.*;
    import java.util.*;
    * @author iaktug
    class StreamGobbler extends Thread
    InputStream is;
    String type;
    StreamGobbler(InputStream is, String type)
    this.is = is;
    this.type = type;
    @Override public void run()
    try
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe)
    ioe.printStackTrace();
    I got the code from the famous when Runtime.exec() won't. I am now suspecting there is something wrong with this part.
    Essentially I want a very simple thing. The external program will run and the parent program will wait for it and get the return value.
    I added the Stream stuff simply to prevent deadlock/hanging.
    I am considering trying to get that out completely.
    The server messages go as follows:....
    Execing |#]
    [#|2009-12-17T09:42:40.499+0400|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=878233;_ThreadName=httpSSLWorkerThread-8082-4;|
    C:\WINDOWS\system32\cmd.exe /start "C:\Documents and Settings\Administrator\Start Menu\Programs\ProTeleCo\QuotEncrypt.appref-ms" "H:\QUOTATIONS\Quotations\In Progress\q00502.xls"1q2w3e|#]
    [#|2009-12-17T09:42:40.499+0400|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=878234;_ThreadName=Thread-923673;|
    OUTPUT>Microsoft Windows [Version 5.2.3790]|#]
    [#|2009-12-17T09:42:40.499+0400|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=878234;_ThreadName=Thread-923673;|
    OUTPUT>(C) Copyright 1985-2003 Microsoft Corp.|#]
    [#|2009-12-17T09:42:40.499+0400|INFO|sun-appserver2.1|javax.enterprise.system.stream.out|_ThreadID=878234;_ThreadName=Thread-923673;|
    OUTPUT>|#]

  • Runtime.exec() for openning a browser

    hey guys!!
    I could open a browser using a runtime.exec(). once i have opened it can i use the process.getOuputStream() method to read what is there in the browser.
    How do i communicate with the browser?
    -karthek

    No, getOutputStream will read only what the program writes to standard output. Typically, GUI programs don't even use standard output. The only way you can communicate with processes started by Runtime.exec that I know of is with the Process class.

  • Issue in using Runtime.exec for service in linux

    Hi,
    I have a problem in executing my own service through java6 on linux operating system.
    I have created a UI (user interface) which contains a button. On clicking that button my own service gets executed successfully. But the problem is that when I close my UI, my service gets stopped. I want that it should keep running even when the UI gets closed.
    I am using this command to start my service in ActionListener:
    Runtime.getRuntime().exec("service myservice start");Am I missing something here?
    Is this the expected behavior of exec()?

    You need to use something along the lines of
            String[] command =
                "bash", "-c", "xemacs > /dev/null 2>&1 &"
            Process p = Runtime.getRuntime().exec(command);
            Thread stderr = new StreamGobbler(p.getErrorStream(), "stderr");
            stderr.setDaemon(true);
            stderr.start();
            Thread stdout = new StreamGobbler(p.getInputStream(), "stdout");
            stdout.setDaemon(true);
            stdout.start();
            int exitCode = p.waitFor();
            System.out.println("Exit code = " + exitCode);where the StreamGobbler class is taken from [http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]. You should read all the 4 sections of this article.

  • Runtime.exec and dialog windows

    I'm using Runtime.exec to launch an exe that may crash due to runtime errors (such as division by zero). When this appens a dialog window shows up, waiting for the user to close it. (I'm running the program under Win98). Is there a way to avoid this?

    I think the only way should be fixing the exe bug!
    There is no way, I suppose.
    Ciao!

  • Runtime.exec not releasing lock on files

    Hi,
    I am using Runtime.exec to launch an external application, a batch file that runs a java program and generates a pdf file on disk. The Runtime.exec is called from inside the callback function of a toolbar button in my UI. Once, the runtime.exec returns, I popup a alert saying "pdf generated successfully". However, when I try to open the pdf that has been generated, I get a message saying the "program is in use by another program" or some such message. Only when I quit my GUI application, Iam able to open the pdf file using acrobat reader.
    Can someone tell me how to solve this problem. I have tried doing proc.waitFor() etc.
    Cheers,
    vidyut

    You can try some of sysinternals tools to look through file sharing information and decide what process is locked file.
    PS Try to ensure that you start java in same process as batch file executes, overthise you just can't ensure it terminates on time.

  • Runtime.exec() with .bat-files in a jar

    Hi All,
    I've written a java-Program, which calls .bat-files per Runtime.exec() for some OS-specific tasks.
    Everything works fine, but if I want to pack the whole code and the .bat-files into several jars, I can't get the bat-files to run. I have tried to get it to work with "getClass().getResource()" as I do the same for my Images, and the returned URL seems to be OK so far (something like jar:file:/c:/test.jar! testpkg/test.bat). I converted this URL into a String and tried to run Runtime.exec() with it, but I always get a Runtime-Exception.
    The String looks exactly like the URL, when I print them to console.
    These bat-files are essential for the application and I would not try to pack them into a jar if I hadn't to distribute this application as a signed applet to unknown users, too.
    I hope there is anyone out there who can tell me if and how it is possible to run an external program out of java, which is packed into a jar, so thanx in advance to any helpful replies.
    acdeka

    You can't run the .bats simply because the shell can't access it. You tell it to run a file that simply doesn't exist in the OS.

  • Runtime.exec--problems writing to external file

    Hi. I went to http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html, and am still quite confused regarding the use of Runtime.exec, for my purposes. I want to decompile a CLASS file using javap, and then write that to a TXT file, for easier reading/input to JAVA. Now, I use the following code (a modification of what I got from http://www.mountainstorm.com/publications/javazine.html, as the "traps" article's sample code is WAY too confusing--they say the compiler had the output sent to text.txt, without even showing how in the source code they did that), but it hangs up. Modifications to the string array cause different results, such as showing the javap help menu, or saying that the class wasn't found, but I think the way I did the array here is right:
    import java.util.*;
    import java.io.*;
    public class Test {
            try {
             String ls_str;
                String[] cmd = {"C:\\j2sdk1.4.2_04\\bin\\javap", "-c", "-classpath", "H:\\Java\\metricTest", "metricTest > blah.txt", ""};
                Process ls_proc = Runtime.getRuntime().exec(cmd);
             // get its output (your input) stream
             DataInputStream ls_in = new DataInputStream(
                                              ls_proc.getInputStream());
             try {
              while ((ls_str = ls_in.readLine()) != null) {
                  System.out.println(ls_str);
             } catch (IOException e) {
              System.exit(0);
         } catch (IOException e1) {
             System.err.println(e1);
             System.exit(1);
         System.exit(0);
    }

    Also, jesie, I realize that's what I need...the only
    problem is, the name "test.txt" is nowhere to be found
    in the source code! lolLooks like I have to explain this, then.
    When you look at a Java program you'll notice that it always has a "main" method whose signature looks like this:public static void main(String[] args)When you execute that program from the command line, it takes whatever strings you put after the class name and passes them to the main program as that array of strings. For example if you run it likejava UselessProgram foo bar hippothen the "java" command takes the three strings "foo", "bar", and "hippo" and puts them into that args[] array before calling the "main" method.
    That means that inside the "main" method in this case, "args[0]" contains "foo", "args[1]" contains "bar", and "args[2]" contains "hippo".
    Now go back to the example and see how it lines up with that.

  • Launching another VM instance using Runtime.exec()

    Hi,
    I have two separate Swing applications, say A and B. I need to launch B from A using Runtime.exec() - essentially I am launching another VM instance from one VM. I am encountering strange problems - B's main window comes up, but GUI doesn't update; sometimes B comes up and GUI updates, but huge memory leaks happen. All the problems with B go away once A is shut down. It almost looks like both the instances of VM are sharing (or competing for) the event thread. Any comments will be of great help. Thanks.
    note: There is no problem launching B if A is a console java application (i.e. no Swing GUI).

    I have recently had the same problem, especially with the part where it seems like the two applications are sharing or competing for the same event thread. Sometimes my application B hangs like it is stuck waiting for an event. Sometimes I can get it to unhang by moving the app window around or minimizing it. If it does break free, all the queued events occur in rapid succession. Anybody have any clue what's going on here? It's pretty annoying.

  • Launch Acrobat Reader with Runtime.exec() under WINNT4, without absolut pat

    Hi,
    I would like to launch Acrobat Reader from a Swing application. This works fine with Runtime.exec() as long as I provide the full path to the AcroRd32.exe. The problem is that I cannot predict the location of this exe on the systems of the users. Athough is in the Windows PATH, since I can launch from the windows execute-dialog, the Runtime.exec() is not able to locate the file.
    How can I solve this problem?
    Thanks a lot!
    Horst

    hey BIJ, thank you so much man. start really does wonders!! i was stuck with invoking a fortran program for last couple of days and now i went through your postings and tried with start. it did work!!
    thanks again,
    bhairampally

  • Launching another Swing application from existing one using Runtime.exec()

    Hi,
    I have two separate Swing applications, say A and B. I need to launch B from A using Runtime.exec() - essentially I am launching another VM instance from one VM. I am encountering strange problems - B's main window comes up, but GUI doesn't update; sometimes B comes up and GUI updates, but huge memory leaks happen. All the problems with B go away once A is shut down. It almost looks like both the instances of VM are sharing (or competing for) the event thread. Any comments will be of great help. Thanks.
    note: There is no problem launching B if A is a console java application (i.e. no Swing GUI).

    Do you have to have the second application running in a seperate VM (process)? If not completely neccesary, then you can just invoke the static main() of the second class. If the type of the second class isn't known untill run-time, you can use Reflection to invoke the method.
    Miguel

  • Runtime.exec() - how to open new window for new program?

    Hi,
    I have searched through the forums but haven't found an answer to this one yet. I am using runtime.exec() to start up a new java program. At first I thought it wasn't running properly but, after checking task manager, I have discovered that the new program I open runs, it is just completely invisible to me. I am wondering how I can get the new program to run in a command window or something where I can monitor it.
    Thank you for your help,
    Drew

    Thank you all. After trying to figure out why the start command wouldn't work in the runtime.exec() call(not an executable - I am a dolt), I tried putting it in a batch file and it worked perfectly. Thanks for your help,
    Drew

  • Set Path to DLLs for Runtime.exec(...)

    I have got a little server application that creates a Process of an external application. (eg Example1.exe or Example2.exe - which one is to be called is configurable via property files)
    All of these external apps rely on a couple of DLLs, that are distributed seperately from the ExampleX.exe.
    So my directory structure looks something like this:
    /Server (contains my java app)
    /Server/dll (contains the dlls)
    /Server/app (contains ExampleX.exe files)
    How do I configure my Runtime.exec(...) so that it can supply a PATH info to my DLLs?
    How about the ...exec(String command, String[] envp, File dir) call?
    -> It already works when the DLLs are in the same dir as my .exe files.
    -> I would like to avoid setting a global PATH to my DLLs in the OS.
    Has anybody already dug itself through this pile?

    Solved my problem, awarded the Dukes to myself! ;-)
    Here's what I did in case somebody else has similar probs:
    The envp Parameter didn't work for me. The Application and the DLLs were found but the Subprocess was somehow screwed up - the Threading didn't work right. (I have no clue how that could be, but whatever...)
    Anyway I could put the path to the DLLs in the PATH info of my startup script for the Java-Server. The subprocess inherits that information.
    I used relative path info there so that nobody has to fiddle around in the script when the application is moved to different directories.
    The only thing you have to do then is to set the present working directory of the subprocess to the location of the server via ...execute(String, String[], File)
    You need to supply thecomplete path to the application in the first String parameter that way but all in all this semms to be the most sensible way to do it.

Maybe you are looking for