在浏览器中显示java输出

在浏览器中显示java输出,java,batch-file,Java,Batch File,我有一个java类,当我们运行时,会执行一个批处理文件。我有一个变量issusccessful(布尔值),它将显示true或false,指示批处理文件是否正确执行其命令。现在,true或false输出仅显示在控制台中。当我键入URL时,我希望它显示在web浏览器上(例如,localhost:8080/runbatchfile) 到目前为止,我有以下代码: RunBatchfile.java @RunWith(SpringRunner.class) @SpringBootTest public c

我有一个java类,当我们运行时,会执行一个批处理文件。我有一个变量
issusccessful
(布尔值),它将显示true或false,指示批处理文件是否正确执行其命令。现在,true或false输出仅显示在控制台中。当我键入URL时,我希望它显示在web浏览器上(例如,
localhost:8080/runbatchfile

到目前为止,我有以下代码:

RunBatchfile.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class RunBatchFile {

@Test
public void RunningBatchCommand() {

    String filePath = "C:/Users/attsuap1/Desktop/test.bat";
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        boolean isSuccessful = true;

        if (exitVal == 0)

        {
            isSuccessful = true;
        } else {
            isSuccessful = false;
        }

        System.out.println(isSuccessful);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}
public class BatchFile extends RunBatchFile {

private static String isSuccessful;

public BatchFile(String isSuccessful) {
    this.isSuccessful = isSuccessful;
}

public static Object getIsSuccessful() {
    System.out.println(isSuccessful);
    return isSuccessful;
  }
}
@RestController
public class BatchFileController {

private static final String template = "Result, %s";
private static String getIsSuccessful;

@RequestMapping("/runbatchfile")
@ResponseBody
public BatchFile batchFile(@RequestParam(value = "result") String result) {
    return new BatchFile(String.format(template, result));
  }
}
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
BatchFile.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class RunBatchFile {

@Test
public void RunningBatchCommand() {

    String filePath = "C:/Users/attsuap1/Desktop/test.bat";
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        boolean isSuccessful = true;

        if (exitVal == 0)

        {
            isSuccessful = true;
        } else {
            isSuccessful = false;
        }

        System.out.println(isSuccessful);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}
public class BatchFile extends RunBatchFile {

private static String isSuccessful;

public BatchFile(String isSuccessful) {
    this.isSuccessful = isSuccessful;
}

public static Object getIsSuccessful() {
    System.out.println(isSuccessful);
    return isSuccessful;
  }
}
@RestController
public class BatchFileController {

private static final String template = "Result, %s";
private static String getIsSuccessful;

@RequestMapping("/runbatchfile")
@ResponseBody
public BatchFile batchFile(@RequestParam(value = "result") String result) {
    return new BatchFile(String.format(template, result));
  }
}
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
这个BatchFile.Java类给了我以下错误:

异常:测试类应该只有一个公共 零参数构造函数 org.junit.runners.BlockJUnit4ClassRunner.validatezerogconstructor(BlockJUnit4ClassRunner.java:171) 在 org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148) 在 org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127) 位于org.junit.runners.ParentRunner.validate(ParentRunner.java:416) ParentRunner.(ParentRunner.java:84)位于 BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:65) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:138) 在 springframework.test.context.junit4.SpringRunner.(SpringRunner.java:49) 在sun.reflect.nativeConstructor附件mpl.newInstance0(本机 方法)在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源) 在sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知)处 源代码)位于java.lang.reflect.Constructor.newInstance(未知源代码) 在 org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) 在 org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 在 org.junit.internal.builders.AllDefaultPossibilityBuilder.runnerForClass(AllDefaultPossibilityBuilder.java:26) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 在 org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

BatchFileController.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class RunBatchFile {

@Test
public void RunningBatchCommand() {

    String filePath = "C:/Users/attsuap1/Desktop/test.bat";
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        boolean isSuccessful = true;

        if (exitVal == 0)

        {
            isSuccessful = true;
        } else {
            isSuccessful = false;
        }

        System.out.println(isSuccessful);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}
public class BatchFile extends RunBatchFile {

private static String isSuccessful;

public BatchFile(String isSuccessful) {
    this.isSuccessful = isSuccessful;
}

public static Object getIsSuccessful() {
    System.out.println(isSuccessful);
    return isSuccessful;
  }
}
@RestController
public class BatchFileController {

private static final String template = "Result, %s";
private static String getIsSuccessful;

@RequestMapping("/runbatchfile")
@ResponseBody
public BatchFile batchFile(@RequestParam(value = "result") String result) {
    return new BatchFile(String.format(template, result));
  }
}
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
Application.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class RunBatchFile {

@Test
public void RunningBatchCommand() {

    String filePath = "C:/Users/attsuap1/Desktop/test.bat";
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        boolean isSuccessful = true;

        if (exitVal == 0)

        {
            isSuccessful = true;
        } else {
            isSuccessful = false;
        }

        System.out.println(isSuccessful);

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}
public class BatchFile extends RunBatchFile {

private static String isSuccessful;

public BatchFile(String isSuccessful) {
    this.isSuccessful = isSuccessful;
}

public static Object getIsSuccessful() {
    System.out.println(isSuccessful);
    return isSuccessful;
  }
}
@RestController
public class BatchFileController {

private static final String template = "Result, %s";
private static String getIsSuccessful;

@RequestMapping("/runbatchfile")
@ResponseBody
public BatchFile batchFile(@RequestParam(value = "result") String result) {
    return new BatchFile(String.format(template, result));
  }
}
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

我是新来的。我尽力了。有人请帮我更正密码。非常感谢。

BatchFile
中创建一个新的构造函数,它将调用方法
contextLoads
。并让您的
BatchFileController
类调用此新构造函数


我还建议您从
RunBatchFile
中删除所有注释,因为这些注释是用于Junits的,这就是您获得异常的原因。

您可以粘贴您获得的异常吗?@Waqas Hi,完成了。您如何调用“contextLoads()”方法?我没有看到它是从“BatchFile”类调用的,这是我在创建BatchFile类之前从教程中学习的第一个创建方法。我应该将contextLoads更改为什么?RunBatchFile.java类单独工作并完成它应该做的事情。但是,现在我必须在浏览器中显示输出,我创建了一个新类BatchFile.java。现在我不知道如何链接这两个文件。这就是我想要的:
在BatchFile中创建一个新的构造函数,它将调用contextLoads方法
,但我不知道如何做。你能在这方面给我多指点吗