Susha Naidu
Susha Naidu

Reputation: 307

Showing java output in browser

I have a java class which when we run, a batch file will be executed. I have a variable isSuccessful (boolean) which will display either true or false indicating if the batch file executed its command correctly or incorrectly. Now, the true or false output is only shown in the console. I want it to be displayed on the web browser when i type in a URL (e.g. localhost:8080/runbatchfile)

So far i have these codes:

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

BatchFile.java

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

This BatchFile.Java class gives me this error:

java.lang.Exception: Test class should have exactly one public zero-argument constructor at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171) at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148) at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127) at org.junit.runners.ParentRunner.validate(ParentRunner.java:416) at org.junit.runners.ParentRunner.(ParentRunner.java:84) at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:65) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:138) at org.springframework.test.context.junit4.SpringRunner.(SpringRunner.java:49) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

BatchFileController.java

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

Application.java

@SpringBootApplication
public class Application {

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

I am new to this. I tried what i can. Someone please help me to correct my codes. Thank you so much.

Upvotes: 1

Views: 93

Answers (1)

user4590342
user4590342

Reputation:

Create a new constructor in BatchFile which will call the method contextLoads. And have your BatchFileController class call this new constructor instead.

I would also suggest removing all the annotations you have from RunBatchFile, since those are for Junits and it is why you're getting the exception.

Upvotes: 1

Related Questions