import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, StringIndexOutOfBoundsException
{
Runtime.getRuntime().exec("cmd /c start C:\\dig-files3\\query3.bat");
}
}
I'm trying to launch a batch file through a java program but I get a 'dig not recognized as an internal or external command ...' message in the cmd screen. However when I double click on the batch file in the window it runs fine. How can I fix this? Here is the batch file's content:
SET /a VAR=0
:HOME
SET /a VAR=VAR+1
IF %VAR%==200000 goto :End
dig @10.3.1.166 6.4.0.3.5.5.5.9.9.9.com. naptr
goto :HOME
:END
Your problem is that you do not have the batch file in the system PATH variable. Insert the path to your batch file into the system PATH and it should work fine
This is probably happening because "dig" has not been added to your PATH variable. Try opening a new terminal window and typing "dig" and it will probably show the same error. You have to go to Control Panel -> System -> System Properties -> Advanced options tab -> Environment variables.
There you have to search for the PATH variable and add, at the end (and after adding ";" to the last command) the full path to "dig" executable (except for the executable itself e.g. c:\foo\bar). Then try again. This environment variable tells Windows to look on the list of paths contained in it, for the executable you are trying to run.
Another solution is to copy over your compiled java file to where the dig executable is located and run it from there.
You should create a file object for the working directory to prevent problems with whitespaces in the path and then use that object to start the batch script:
There's also a flaw in your batch script: You probably want to write
SET /a VAR=%VAR%+1
so that%VAR%
gets evaluated before incrementing it.Ok, there may another way to fix this but this is how I did it. I am using Eclipse and I copied the dig application to the project directory C:\User\username\workspace\projectName