I need to run a specific file (start.bat), via java code. is it even possible?

  • one
    Yes it is possible. - pavel
  • one
    Can you clarify the question, what exactly did you want to ask, is it possible or how to launch it? - Naumov

2 answers 2

Yes it is possible. For example:

Runtime.getRuntime().exec("cmd start start.bat"); 

Where start.bat is the full path to the start.bat file.

  • Thank you! And what to do in cases of Linux? - SCpacker
  • @SCpacker, And where does Linux? - s8am

This can also be done via ProcessBuilder . Unfortunately, I can’t say exactly how it will be for Windows , but on a Mac executing a command, for example, ls -i in the terminal will look like this:

  String[] command = new String[]{"/bin/bash", "-c", "ls", "-i"}; ProcessBuilder probuilder = new ProcessBuilder(command); Process process = probuilder.start(); try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = br.readLine()) != null) System.out.println(line); } try { int exitCode = process.waitFor(); if (exitCode != 0) System.out.println("error code is not zero"); } catch (InterruptedException e) { e.printStackTrace(); } 

I think, by analogy, you can substitute the commands you need.