목차 :::

 

1. Using Runtime.exec()

2. Launch a Windows CMD (or BAT) file and retrieve the errorlevel or exitcode

3. Launch a Unix script

4. Using the ProcessBuilder

5. Windows rundll32 utility

6. PDF (Windows only)

7. PDF (Mac only)

8. Path to executable with spaces in them

9. VBSCRIPT

10. HTML Help (Windows only)

11. Start Excel

12. Start a Windows application under another account

13. Windows : execute something in Program Files

 

 

 

Using Runtime.exec()

This example will

capture

the output (from stdio) of an external program.

package com.rgagnon.howto;

import java.io.*;

public class Exec {
  public static void main(String args[]) {
    try {
      String line;
      Process p = Runtime.getRuntime().exec("cmd /c dir");
      BufferedReader bri = new BufferedReader
        (new InputStreamReader(p.getInputStream()));
      BufferedReader bre = new BufferedReader
        (new InputStreamReader(p.getErrorStream()));
      while ((line = bri.readLine()) != null) {
        System.out.println(line);
      }
      bri.close();
      while ((line = bre.readLine()) != null) {
        System.out.println(line);
      }
      bre.close();
      p.waitFor();
      System.out.println("Done.");
    }
    catch (Exception err) {
      err.printStackTrace();
    }
  }
}

The next example, launch CMD.EXE, grab stdin/stdout and push to stdin command to be interpreted by the shell.

    String line;
    OutputStream stdin = null;
    InputStream stderr = null;
    InputStream stdout = null;

      // launch EXE and grab stdin/stdout and stderr
      Process process = Runtime.getRuntime ().exec ("/folder/exec.exe");
      stdin = process.getOutputStream ();
      stderr = process.getErrorStream ();
      stdout = process.getInputStream ();

      // "write" the parms into stdin
      line = "param1" + "n";
      stdin.write(line.getBytes() );
      stdin.flush();

      line = "param2" + "n";
      stdin.write(line.getBytes() );
      stdin.flush();

      line = "param3" + "n";
      stdin.write(line.getBytes() );
      stdin.flush();

      stdin.close();

      // clean up if any output in stdout
      BufferedReader brCleanUp =
        new BufferedReader (new InputStreamReader (stdout));
      while ((line = brCleanUp.readLine ()) != null) {
        //System.out.println ("[Stdout] " + line);
      }
      brCleanUp.close();

      // clean up if any output in stderr
      brCleanUp =
        new BufferedReader (new InputStreamReader (stderr));
      while ((line = brCleanUp.readLine ()) != null) {
        //System.out.println ("[Stderr] " + line);
      }
      brCleanUp.close();

 

Launch a Windows CMD (or BAT) file and retrieve the errorlevel or exitcode

 

// win xp
import java.io.*;
public class CmdExec {
  public static void main(String argv[]) {
    try {
      String line;
      Process p = Runtime.getRuntime().exec("test.cmd");
      p.waitFor();
      System.out.println(p.exitValue());
    }
    catch (Exception err) {
      err.printStackTrace();
    }
  }
}

 

test.cmd (set the errorlevel manually)

 

@echo hello world
@exit 42

 

test.cmd (set the errorlevel 1 (problem detected)

 

@java -garbage

 

test.cmd (set the errorlevel 0 (execution Ok)

 

@java -version

 

Launch a Unix script

 

String[] cmd = {"/bin/sh", "-c", "ls > hello"};
Runtime.getRuntime().exec(cmd);

 

Using the ProcessBuilder

Since 1.5, the ProcessBuilder class provides more controls overs the process to be started. It's possible to set a starting directory.

import java.io.*;
import java.util.*;

public class CmdProcessBuilder {
  public static void main(String args[])
     throws InterruptedException,IOException
  {
    List<String> command = new ArrayList<String>();
    command.add(System.getenv("windir") +"system32"+"tree.com");
    command.add("/A");

    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();
    builder.directory(new File(System.getenv("temp")));

    System.out.println("Directory : " + System.getenv("temp") );
    final Process process = builder.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    System.out.println("Program terminated!");
  }
}

 

Windows rundll32 utility

 

Windows File association


Any program using the Windows file association mechanism can be started with the rundll32 utility.

// "file" is the filename of the data file
//  ex. myresume.doc
//  to start Word if the doc extension is associated with it.
Runtime.getRuntime().exec
  ("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());

 

See also this HowTo about the new Desktop API, the recommended solution (but you need JDK1.6).
See also this one to open the default browser.

The following example start a Dial-up connection on the Win plateform :

[Dialup.java]
public class Dialup {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime()
      .exec("rundll32.exe rnaui.dll,RnaDial MyConnection");
    p.waitFor();
    System.out.println("Done.");
  }
}

The "MyConnection" is the DUN and it's case sensitive.

You still need to press ENTER to CONNECT, there is an option in the Connection properties to connect automatically.

On NT and W2K, rnaui.dll is not available. Use rasdial.exe instead.

rasdial "connection name"
rasdial "connection name" /d to drop
rasdial /? for more options

 

PDF (Windows only)

 

public class ShowPDF {
  public static void main(String[] args) throws Exception {
    Process p =
      Runtime.getRuntime()
        .exec("rundll32 url.dll,FileProtocolHandler c:/pdf/mypdf.pdf");
    p.waitFor();
    System.out.println("Done.");
  }
}

 

PDF (Mac only)

 

public class ShowPDF {
  public static void main (String[] args) throws Exception{
    Process p = Runtime.getRuntime().exec("open /Documents/mypdf.pdf");
  }
}

 

More runddl32 examples


 

Path to executable with spaces in them

 

You can include a path for the program to be executed. On the Win plateform, you need to put the path in quotes if the path contains spaces.

public class Test {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(
       ""c:/program files/windows/notepad.exe"");
    p.waitFor();
  }
}

 

If you need to pass arguments, it's safer to a String array especially if they contain spaces.

String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);

If using the start command and the path of the file to be started contains a space then you must specified a title to the start command.

String fileName = "c:ApplicationsMy Documentstest.doc";
String[] commands = {"cmd", "/c", "start", ""DummyTitle"",fileName};
Runtime.getRuntime().exec(commands);

 


 

VBSCRIPT

 

// Win9x
Runtime.getRuntime().exec("start myscript.vbs");

// WinNT
Runtime.getRuntime().exec("cmd /c start myscript.vbs");

or

// with a visible console
Runtime.getRuntime().exec("cscript myscript.vbs");

// with no visible console
Runtime.getRuntime().exec("wscript myscript.vbs");

 

HTML Help (Windows only)

 

Runtime.getRuntime().exec("hh.exe myhelpfile.chm");

 

Start Excel

 

import java.io.IOException;

class StartExcel {
    public static void main(String args[])
        throws IOException
    {
        Runtime.getRuntime().exec("cmd /c start excel.exe");
    }
}

To load a worksheet

import java.io.IOException;

class StartExcel {
    public static void main(String args[])
        throws IOException
    {
        String fileName = "c:tempxlstest2.xls";
        String[] commands = {"cmd", "/c", "start", ""DummyTitle"",fileName};
        Runtime.getRuntime().exec(commands);
    }
}

It's important to pass a

dummy title

to the Windows start command where there is a possibility that the filename contains a space. It's a

feature

.

Start a Windows application under another account

You use the RUNAS command from the command line to start an application under another account (not available with XP Home edition). There are many switches that can enhance the behaviour of RUNAS. Typing "runas /?" from the command prompt gets you all the options.

  String  commands [] = new String [] {
    "CMD.EXE",
    "/C",
    "RUNAS /profile  /savecred /user:"
    + "administrator"
    + " " + "regedit.exe"
  };

  Runtime.getRuntime().exec(commands);

 

/SaveCred

option allows you to save a password for that account and then reuse it later. For example, The command

runas /savecred /user:administrator regedit.exe

prompts for the password, and then Regedit runs. Next time you use the same command, there is no password prompt.

One potential problem is that when /SaveCred saves the credentials it saves it for whenever RUNAS invokes that user account. This can be a huge security risk so be careful using it!

RUNAS capability can be disabled by editing the Registry or by disabling the RUNAS or Secondary Logon Services. The appropriate registry key is HKLMSOFTWAREMicrosoftWindowsCurrentVersionpoliciesExplorer, create a new DWORD value named HideRunAsVerb and assign it a value of 1 to disable Run as.

RUNAS doesn't work when used from a Windows service.

Windows : execute something in Program Files

We want to execute the textpad editor located in

C:Program FilesTextPad 4

but without hard coding the path since it can be different for a localized version of Windows.

We simply extract to environnment variable called %programfiles% and build the complete path from there.

[JDK1.5]

public class Exec {
    static String WIN_PROGRAMFILES = System.getenv("programfiles");
    static String FILE_SEPARATOR   = System.getProperty("file.separator");

    public static void main(String[] args) throws Exception {
     String[] commands =
       {"cmd.exe",
        "/c",
        WIN_PROGRAMFILES
        + FILE_SEPARATOR
        + "textpad 4"
        + FILE_SEPARATOR + "textpad.exe"};
     Runtime.getRuntime().exec(commands);
    }
}

NOTE : Prior Vista, System folders were localized on disk like

C:Program Files

->

C:Archivos de programa

on the Windows with the Spanish localization. Since Vista, System Folders always exists with the english name BUT when viewed through Explorer, the localized name is shown. See

http://msmvps.com/blogs/carlosq/archive/2007/02/12/windows-vista-junctions-points-mui-and-localized-folder-names.aspx

 

 

 

번호 제목 글쓴이 날짜 조회 수
347 java.util.Optional 에서의 map() 함수의 이해 황제낙엽 2026.06.24 666
346 linux(ubuntu) 의 jdk 21 이 설정된 환경에서 jdk 17 로 다운그레이드 절차 황제낙엽 2025.09.22 1532
345 [ChatGPT] javax.* 패키지와 jakarta.* 패키지의 혼합 사용 방안 황제낙엽 2025.04.15 2459
344 [ChatGPT] *.servlet.http.HttpServlet 클래스를 사용할 수 있는 주요 WAS 목록 황제낙엽 2025.04.14 2038
343 [ChatGPT] JDBC API에서 java.sql과 javax.sql 패키지의 차이점 황제낙엽 2025.01.31 2218
342 jakarta.servlet 패키지를 이용한 File Upload 구현 (2024.03.03, Servlet 5.0) 황제낙엽 2025.01.02 2310
341 [ChatGPT] Java EE가 Jakarta EE로 전환 황제낙엽 2025.01.01 2525
340 [ChatGPT] File.delete() 함수로 파일 삭제가 되지 않는 경우 황제낙엽 2024.12.10 2809
339 [ChatGPT] <jsp:include>에서 flush="true"를 설정시의 몇 가지 문제점 과 대안 황제낙엽 2024.07.19 2493
338 [Copilot] JSP, Servlet, web.xml, mysql 한글 설정 황제낙엽 2024.07.12 2555
337 Base64 클래스의 encode, decode 에서 URL-safe 황제낙엽 2024.07.10 2624
336 [Copilot] JSP 에서 다른 페이지를 현재 페이지에 포함시키는 문법 (include) 황제낙엽 2024.06.26 2570
335 Exception 출력에 대한 고찰 황제낙엽 2024.06.10 2198
334 google-auth-library-oauth2-http 라이브러리 다운로드 황제낙엽 2023.11.19 4025
333 firebase-admin-java 라이브러리 다운로드 (firebase admin sdk library) 황제낙엽 2023.11.19 2805
332 Enum 활용 (개인블로그, Effective Java) file 황제낙엽 2023.11.02 2759
331 Enum 활용 (우아한기술블로그) file 황제낙엽 2023.11.02 2790
330 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 2508
329 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 3336
328 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 2935