Having trouble running my program via the getRunti

2019-08-06 16:49发布

问题:

I have the following class :

After compiling my CreateTexts.java class, I want to run it via getRuntime (calling it through Eclipse ). So I run this class

public class RuntimeDemo {

   public static void main(String[] args) {
   try {

   // create a new array of 2 strings
   String[] cmdArray = new String[2];

   // first argument is the program we want to open
   //C:\Program Files\Java\jdk1.6.0_20\bin
   cmdArray[0] = "C://Program Files//Java//jdk1.6.0_20//bin//java";

   // second argument is a txt file we want to open with notepad


  cmdArray[1] = "C://logback//CreateTexts";



   // create a process and execute cmdArray and currect environment
   Process process = Runtime.getRuntime().exec(cmdArray,null);


   } catch (Exception ex) {
   ex.printStackTrace();
   }

   }
}

But I'm not seeing the output file being created. Any tips appareciated thanks

回答1:

The way you are running the program is wrong.

Let's suppose you CreateTexts is inside the package logback. And the file is located in "C://logback//CreateTexts", then you have to cd to C: and then execute the program as java logback.CreateTexts.

Just directly calling the file using absolute path from any directory wont work.