0

I am writing program for tests automation purpose. That program would load list (names) of batch files from a folder program is in. Then I could select individual test or all and commence execution. Each batch file connects to the oracle, does some updates and more, and then selected output is spooled to a csv file and output file is placed to the local drive.

I wrote a program that loads batch file into the java program using ProcessBuilder, but when I tried to execute batch file it failed at opening sql script from bat file.

ProcessBuilder pb = new ProcessBuilder(System.getProperty("user.dir")+"//src//test.bat");
try {
    pb.start();
    Process process = pb.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    System.out.println(sb);

bat file (very simple version):

@echo ON
set /A var = 100000000
sqlplus -s login/password@server @script.sql %var%
type results_*.csv > "results.csv"
del results_*.csv

When I'm debugging, the whole thing fails at opening sql script.

SP2-0310: can't open file "script.sql"

But I don't want bat file being opened and executed inside my program. I want my java program to launch windows command line that would execute my bat file. Is there any way to achieve this goal Or if there's no way - how could I parse that script file inside my program (I'd rather staw away from this path).

Compo
  • 36,585
  • 5
  • 27
  • 39
  • What do you think is the difference between "bat file being opened and executed inside my program" and " java program to launch windows command line that would execute my bat file"? – tgdavies Apr 23 '23 at 12:00
  • When bat file is being opened in my java program it does not open command line, but starts to assigning variables, connects to oracle and tries to open script.sql - the last one fails. I want my program to simply launch command line with that bat file. – beastofmisery Apr 23 '23 at 12:09
  • 1. You can remove your BufferedReader, and the loop that reads from it, and replace all of that with `pb.inheritIO();` (**before** you call pb.start()). 2. You need to learn what a [current working directory](https://en.wikipedia.org/wiki/Working_directory) is. A relative file name like `script.sql` depends on the current directory of the process that refers to it. – VGR Apr 23 '23 at 12:13
  • As @VGR has virtually suggested, try using the full path to that script.sql file – g00se Apr 23 '23 at 13:19
  • @g00se I didn't solve the problem. Debugging mode in eclipse still can't open sql file. (While command line has no problem with either (at)script.sql or C:\Users\me\eclipse-workspace\my_program\src\script.sql). – beastofmisery Apr 23 '23 at 14:41
  • Make sure the script file is not open in any other editor or app – g00se Apr 23 '23 at 14:43
  • @VGR removig BufferReader and replacing it with pb.inheritIO(); did not work. Program, despite having access to full path of bat file, and bat file having access to full path to script.sql, still fails to done anything (besides assigning variables and connecting to oracle). – beastofmisery Apr 23 '23 at 14:48
  • *no problem with either (at)script.sql or C:\Users\me\eclipse-workspace\my_program\src\script.sql).* No, it should be @C:\Users\me\eclipse-workspace\my_program\src\script.sql in the batch file – g00se Apr 23 '23 at 14:48
  • @g00se tried your solution and also closed everthing but Eclipse and program still fails to open sqript file. Is the any possibility I could refrain from opening bat file inside java program and just force program to launch test.bat in command line? Just like I would double click test.bat. – beastofmisery Apr 23 '23 at 14:53
  • Well you could of course execute the sqlplus command directly from `ProcessBuilder` – g00se Apr 23 '23 at 15:03
  • I'm not quite sure what you're referring to at that link. Also nothing is being "parsed" in your implementation – g00se Apr 23 '23 at 15:18
  • @g00se https://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application link I deleted. I'm reffering to not using ProcessBuilder but getRuntime(), but it still somehow fails to open script.sql, even though it launches command line. – beastofmisery Apr 23 '23 at 15:25
  • 1
    `getRuntime` isn't going to help you. Just view it as a inferior ancestor of `ProcessBuilder`. Try something like `ProcessBuilder pb = new ProcessBuilder("sqlplus", "-s", "login/password@server", "@script.sql", "100000000"); pb.directory(new File("C:/Users/me/eclipse-workspace/my_program/src")); pb.inheritIO(); pb.start();` You can collect the output if and when that works for you – g00se Apr 23 '23 at 15:52

0 Answers0