×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Jabber's toy code--001---Start an external process from within Java virtual machine

Let me say that we want to launch NotePad from with JVM. Here is the toy code:

public class NotepadLauncher{
public static void main(String args[]) throws Exception{
Runtime rt = Runtime.getRuntime();
Process p=rt.exec("notepad.exe");
p.waitFor();
}
}

Here are some questions: 1) Can we designate Notepad to open a specific file, say, abc.txt? 2) What is the effect of "p.waitFor()" ? 3) Do we need set PATH for notepad at windows machine? Is this a good example to teach people the significane of PATH in the programming? Many people have done several year's programming but they still have no sense of the environmental variable "PATH".
Sign in and Reply Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Jabber's toy code--001---Start an external process from within Java virtual machine
    Let me say that we want to launch NotePad from with JVM. Here is the toy code:

    public class NotepadLauncher{
    public static void main(String args[]) throws Exception{
    Runtime rt = Runtime.getRuntime();
    Process p=rt.exec("notepad.exe");
    p.waitFor();
    }
    }

    Here are some questions: 1) Can we designate Notepad to open a specific file, say, abc.txt? 2) What is the effect of "p.waitFor()" ? 3) Do we need set PATH for notepad at windows machine? Is this a good example to teach people the significane of PATH in the programming? Many people have done several year's programming but they still have no sense of the environmental variable "PATH".
    • Let me try: 1. yes "notepad.exe abc.txt". 2. wait until the notepad app closed. 3. yes.
    • I have tried
      Good example.
      I just tried this small assisment. Hope I can learn more from you.
      1. I did like this

      public static void main(String args[]) throws Exception{
      Runtime rt = Runtime.getRuntime();
      Process p=rt.exec("notepad.exe c:\\users\\file\\java\\abc.txt");
      p.waitFor();
      }

      2. the waitFor will cause the current thread to wait, if necessary, until the process represented by this Process object has terminated. So the program will pause until you finished with notepad

      3. no need to set path for notepad in Windows. It's under the default Window directory.

      Question: If I am running another program, say "catchme.exe", I should either add the path to exec or put the path in PATH variable, right?
    • Both egg and hailstorm got 100 scores. Because having gone through tough tests, Canadian programmers are more skillful than their American counterparts.