使用ProcessBuilder运行.java文件 [英] Run a .java file using ProcessBuilder

查看:303
本文介绍了使用ProcessBuilder运行.java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名在Windows XP上使用Eclipse工作的新手程序员,我需要运行多个进程(这将是一个多计算机系统的模拟)。我最初的hackup使用了多个线程到多个类,但现在我正在尝试用进程替换线程。

I'm a novice programmer working in Eclipse on Windows XP, and I need to get multiple processes running (this is going to be a simulation of a multi-computer system). My initial hackup used multiple threads to multiple classes, but now I'm trying to replace the threads with processes.

从我的阅读中,我发现ProcessBuilder是要走的路。我已经尝试了下面看到的很多很多版本的输入,但在我的生活中无法弄清楚如何正确使用它。我试图运行我以前创建的.java文件作为类(我已经修改)。我最终只是制作了一个虚拟的test.java来确保我的进程正常工作 - 它唯一的功能是打印它运行。

From my reading, I've gleaned that ProcessBuilder is the way to go. I have tried many many versions of the input you see below, but cannot for the life of me figure out how to properly use it. I am trying to run the .java files I previously created as classes (which I have modified). I eventually just made a dummy test.java to make sure my process is working properly - its only function is to print that it ran.

这两个文件的代码是下面。我正确使用ProcessBuilder吗?这是读取子进程输出的正确方法吗?任何帮助将不胜感激。

My code for the two files are below. Am I using ProcessBuilder correctly? Is this the correct way to read the output of my subprocess? Any help would be much appreciated.


  • David

编辑:解决方案是声明ProcessBuilder(java.exe, - cp,bin,Broker.test);

The solution is to declare ProcessBuilder("java.exe","-cp","bin","Broker.test");

primary过程

package Control;
import java.io.*;
import java.lang.*;

public class runSPARmatch {

/**
 * @param args
 */
public static void main(String args[]) {
    try {       
        ProcessBuilder broker = new ProcessBuilder("javac.exe","test.java","src\\Broker\\");
        Process runBroker = broker.start();

        Reader reader = new InputStreamReader(runBroker.getInputStream());
        int ch;
        while((ch = reader.read())!= -1)
            System.out.println((char)ch);
        reader.close();

        runBroker.waitFor();

        System.out.println("Program complete");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

subprocess

subprocess

package Broker;

public class test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("This works");
    }
}


推荐答案

你正在调用.java文件上的java编译器,这不会运行该类。您可能想要做的是在.class文件上运行java.exe。 (例如java.exe -cp ./bin Broker.test,假设你的类文件是./bin)

You are calling the java compiler on the .java file, this will not run the class. What you probably want to do is running java.exe on your .class file. (ie something like "java.exe -cp ./bin Broker.test", assuming your class files are in ./bin)

这篇关于使用ProcessBuilder运行.java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆