简体   繁体   中英

Running a bat file from source folder in java

I'm trying to run a .bat file in java, but I'm using the specific path(C:..). I was hoping to make it run a bat file inside my src folder in java. How should I code it? this is my current code:

run.exec("cmd start /c C:\Users\mico\workspace\ANTLR java\src\jython2.5.2");

and I also want to put the source file(the path to where the file is saved)

this is the cmd command I'm using:

C:\Users\mico\workspace\ANTLR java\src\jython2.5.2\jython C:\Users\mico\workspace\ANTLR java\src\save.py

How do I execute the entire command while not making it a static path and be able to load the file?

First thing is that you need to have the PWD of your program be set to a known location (the path above the src for example). When you start your jython/ANTLR, you can use ~dp0 and the /D switch on start to do that. The second part is that once you know the PWD, you can just use a relative path within your java source.

You can define the file's path in Autoexec.bat file by appending path line or follow this steps.

  1. Right Click on My Computer
  2. Select Advance Tab
  3. Click on Environment Variable Button
  4. In the second List ( System Variables ) Search for Path and Click on Edit Button
  5. A New Dialog box will open with two Textbox, in the second textbox ( Variable Value ), go to last position and add one ; ( semicolon ) and then add your .bat file's path
  6. Now Execute your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM