2015-07-10 34 views
1

我有下面的类:遇到问题通过getRuntime运行我的程序()在Java中

编译我的CreateTexts.java下课后,我想通过getRuntime运行它(通过Eclipse中调用它)。所以我跑这个类

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(); 
    } 

    } 
} 

但我没有看到输出文件被创建。任何提示appareciated谢谢

+0

我相信我需要使用ProcessBuilder类,而不是这种方式 – Coffee

+0

为什么不简单地在jthis代码中调用批处理文件,让批处理文件执行运行'CreateTexts'代码的工作? – Arvind

回答

1

你运行程序的方式是错误的。

假设您CreateTexts在包logback内。该文件位于"C://logback//CreateTexts",然后您必须cdC:,然后执行程序java logback.CreateTexts

只是直接从任何目录使用绝对路径调用文件不会工作。

+0

嗯,但我怎么能够从Eclipse(这是在一些随机目录中)编译它? – Coffee

+0

或者我猜这是一个Java安全机制或smth? – Coffee

+0

http://stackoverflow.com/questions/3415507/how-can-i-compile-and-run-a-java-class-in-a-different-directory - 我认为这澄清 – Coffee

相关问题