简体   繁体   English

从在另一个Java程序中运行的Java程序获取I / O

[英]Getting I/O from a java program running in another java program

I have two Java programs, called A and B. A looks like this 我有两个Java程序,分别称为A和B。A看起来像这样

Scanner deusex = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
String again = "Y";

while (again == ("Y"))
{
out.println("Enter a string.");
String user1 = deusex.next();
list.add(user1);
 out.println("Input again? [Y/N]");
        again = deusex.next();
        if (again.equalsIgnoreCase("Y"))
        again = "Y";

}
out.println("Your elements: " + list);
out.println("Delete elements? [Y/N]");
String y = deusex.next();

if (y.equalsIgnoreCase("y"))
{ 
String again1 = "Y";
out.println("Printing outputs...");
  out.println(list);
while (again1 == ("Y"))
{
out.println("Enter index of element to delete [0,1,2...]");
  int user3 = deusex.nextInt();
  list.remove(user3);
  out.println("Printing outputs...");
  out.println(list);
   out.println("Input again? [Y/N]");
        again = deusex.next();
        if (again.equalsIgnoreCase("Y"))
        again1 = "Y";
        else System.exit(0);
}
}
else { out.println("Bye."); };
}

} }

I have uploaded A in a .txt file to google drive. 我已将.txt文件中的A上传到了Google驱动器。 My program B is written to download the file then save it as a .java file. 编写我的程序B来下载文件,然后将其另存为.java文件。 I then compile it and try to run it. 然后,我编译它并尝试运行它。 This is the part of code B that is important: 这是代码B的重要部分:

System.out.println("Enter anything to compile and run.");
String anything1 = Gooch.next();
String line = null;
try 
{ 
Process pleasecompile = new ProcessBuilder("javac", "A.java").start();
Process pleaserun = new ProcessBuilder("java", "A").start();
BufferedReader input = new BufferedReader(new InputStreamReader(pleaserun.getInputStream()));
BufferedWriter output= new BufferedWriter(new OutputStreamWriter(pleaserun.getOutputStream()));
while ((line = input.readLine()) != null) {
    System.out.println(line);
    output.write(300);
    }
pleaserun.waitFor();
input.close();
}
catch (Exception e) 
{
  e.printStackTrace();
  }

Running this code for B only prints out the first line of A. I want to be able to run A in its entirety and get and give inputs and outputs. 为B运行此代码只会打印出A的第一行。我希望能够完整地运行A并获取并提供输入和输出。 I figured out so far that I need to manipulate the buffered readers with some loops or processes to do this, but I have no idea how. 到目前为止,我已经弄清楚需要通过一些循环或进程来操作缓冲的读取器,但是我不知道该如何做。

I know that if I just compile A.java with Process pleasecompile I can directly call the class afterwards using something like A.main(String[0]);. 我知道,如果我仅使用Process pleasecompile编译A.java,则以后可以使用类似A.main(String [0]);的类直接调用该类。 The issue is that the A.class file does not exist at compile time, only after being downloaded and compiled itself, so my B program gives me a cannot find symbol error for A.main when compiling. 问题是A.class文件在编译时才存在,只有在下载并编译完之后才存在,所以我的B程序给我一个A.main编译时找不到符号错误的提示。 My question is whether I can set something up that will call the class for me after compiling A in B without the error, if not resorting to manipulating the readers. 我的问题是,如果不求助于读者,我是否可以设置一些东西来在B中编译A之后没有错误地为我调用该类。

My suggestion is to use reflection. 我的建议是使用反射。 As per the code class B can compile and run. 按照代码类别B可以编译并运行。 When B runs, the pleaseCompile method will compile the class A. why don't you go for reflection that is used to load a class at runtime. 当B运行时,pleaseCompile方法将编译类A。为什么不进行反射,该反射用于在运行时加载类。

You can probably visit this link tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html fir tutorials on reflection 您可能可以访问此链接tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html关于反射的冷杉教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM