简体   繁体   English

如何创建运行另一个 java 程序的 java 程序,该程序将从文本文件中接收输入?

[英]How to create a java program that runs another java program which will recieve input from a text file?

I am writing a simple online judge, thus I need a way to test the code a user has written.我正在编写一个简单的在线法官,因此我需要一种方法来测试用户编写的代码。 The flow is as follows:流程如下:

  1. User writes a program that tries to solve the task which was given to him.用户编写了一个程序,试图解决交给他的任务。 For example a program to calculate the square of a number.例如一个计算数字平方的程序。
  2. The program is then given an input file (to be precise there will be many such files for each test case there is one), for example input.txt and then the program generates some output(s) which will be tested against the expected output(s) to determine if everything is correct.然后给程序一个输入文件(准确地说,每个测试用例会有很多这样的文件),例如input.txt然后程序生成一些输出,这些输出将针对预期的输出进行测试(s) 确定一切是否正确。

So let's say that an user has written the following code to solve a problem to find the square of a number:因此,假设用户编写了以下代码来解决求数字平方的问题:

import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(n * n);
    }
}

If I run this program from the terminal, I can do this:如果我从终端运行这个程序,我可以这样做:

$ javac B.java
$ java B < input.txt

Where for example input.txt contains:例如input.txt包含:

5

The program will execute correctly.该程序将正确执行。 What I need to do now is to check that output against the correct output (that step isn't the problem).我现在需要做的是检查 output 与正确的 output (该步骤不是问题)。 However I need to automate this task and for this I've read these answers:但是我需要自动化这个任务,为此我已经阅读了这些答案:

The solution in [1] is preferred, however either solution fails to function properly, namely the issue is here: [1] 中的解决方案是首选,但是任一解决方案都无法正确地 function,即问题在这里:

// omitted most of the code for brevity, similar code as in [2]
public class A {

    public static void main(String[] args) {
        // Fails to pass input.txt to Scanner from class B i.e. that file is being ignored
        processRun = Runtime.getRuntime().exec("java B < input.txt");
    }
}

The issue is that input.txt is completely ignored here and that now the Scanner in class B is waiting indefinitely to receive an input.问题是input.txt在这里完全被忽略了,现在 class B 中的扫描仪正在无限期地等待接收输入。 Is there a way to properly handle this ie to feed input.txt to class B through Java code?有没有办法正确处理这个问题,即通过 Java 代码将input.txt提供给 class B?

I have found a way to do it, albeit it's a workaround/hack:我找到了一种方法来做到这一点,尽管它是一种解决方法/黑客:

// omitted most of the code for brevity, similar code as in [2]
public class A {

    public static void main(String[] args) {
        // Fails to pass input.txt to Scanner from class B i.e. that file is being ignored
        processRun = Runtime.getRuntime().exec("/bin/sh check.sh");
    }
}

echo `java B < input.txt`

Doing things this way works.以这种方式做事是有效的。

Maybe a little late.也许有点晚了。 but try ProcessBuilder但尝试 ProcessBuilder

 ProcessBuilder builder = new ProcessBuilder("java", "B");
 builder.redirectInput(new File("input.txt"));
 try {
         processRun = builder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

暂无
暂无

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

相关问题 是否可能有一个程序可以运行另一个程序(.java文件)中的代码并自动调试每一行 - Is it possible to have a program that runs code from another program (.java file) and automatically debugs each line Java程序从文本文件读取输入并进行相应的修改 - Java Program read input from a text file and modify it accordingly 使用DrJava管道连接(从文本文件重定向输入)到Java程序 - Piping (redirecting input from a text file) into a Java program using DrJava 如何使用Bourne Shell将Java程序的输入传递给另一个程序 - How to pipe input from a Java program to another using Bourne Shell 如何运行服务器的批处理文件(永久运行)并在不关闭该批处理的情况下将控制权返回给Java程序 - How to run a batch file of server(which runs forever) and get the control back to the java program without closing that batch IO:同时从C ++程序和另一个Java程序写入和读取相同的文本文件吗? - IO : Writing and reading to the same text file from a C++ program and from another Java program simultaneously? 如何从Eclipse中的另一个Java程序执行Java程序 - How to execute a java program from another java program in eclipse 如何从另一个 java 程序独立运行 java 程序? - how to run a java program from another java program independently? 如何从另一个Java程序启动Java程序? - How to launch a java program from another java program? 从另一个 Java 程序运行一个 Java 程序 - Running a java program from another java program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM