Im getting “dig is not recognized as an internal o

2019-03-02 14:06发布

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

4条回答
▲ chillily
2楼-- · 2019-03-02 14:37

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

查看更多
3楼-- · 2019-03-02 14:39

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-02 14:39

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:

File workdir = new File("C:\\dig-files3");
Runtime.getRuntime().exec("query3.bat", null, workdir);

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.

查看更多
别忘想泡老子
5楼-- · 2019-03-02 14:46

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

查看更多
登录 后发表回答