Nitin Kumar Nitin Kumar - 23 days ago 7
Java Question

How can I run a bat file which is in my Project's package, Without using the full path

I wanted to know that how can I run a bat file of my Project's package, Without using the full path as for different Computers the file path will be different.

not like this:

"C:\\Users\\Nitin\\Desktop\\Run.bat"


Instead something like we use in getResource() method. That is:

"/Images/Icon.png"


Simply the package name and the file name. I tried with this type of filepath but got error saying file not exists.

I want to know because in every computer that path will get changed (username, drive etc. or may be file will not be present there)

So how can I use the Bat file that is in my package, instead of a full path with drive name, username and all.

Answer

To run multiple commands in one cmd command, you can see the answer on this question.

To get the output, you can use this :

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
   builder.append(line);
   builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
Comments