1

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.

5
  • Can you please paste the exception that you are getting? Commented Nov 30, 2017 at 3:45
  • @Waqas Hi, done it Commented Nov 30, 2017 at 4:00
  • How are you calling your "contextLoads()" method? I don't see it being called from the "BatchFile" class Commented Nov 30, 2017 at 4:10
  • That was the method i created first which i learnt from a tutorial before creating BatchFile class. What should i change the contextLoads to? Commented Nov 30, 2017 at 4:24
  • The RunBatchFile.java class alone works and does what it is supposed to do. However since now i have to show the output in a browser, i created a new class BatchFile.java. Now i am not sure how to link these two files. Commented Nov 30, 2017 at 4:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what i want: Create a new constructor in BatchFile which will call the method contextLoads but i am not sure how to do it. Can you please guide me abit more in this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.