使用Java的ProcessBuilder运行批处理文件时无法识别网络驱动器。

huangapple go评论62阅读模式
英文:

Running Batch File Using Java ProcessBuilder does not recognized Network drive

问题

我有一个使用ProcessBuilder来执行动态批处理文件的Java程序(jar)。

Process process = (new ProcessBuilder(commands)).start();

我有一个简单的批处理文件,内容如下,其中Z --> 是网络驱动器。假设批处理文件的位置是my_batch.bat。

copy Z:\Download\*.csv D:\Download\

如果我从命令提示符中执行此批处理文件,它可以成功运行。但如果我使用Java程序来执行,程序就无法正常工作(没有任何动作)。但如果我将批处理文件更改如下,它就可以正常工作。E-> 是普通驱动器(不是网络驱动器)。

copy E:\Download\*.csv D:\Download\

我不知道为什么会出现这种情况,请帮忙解决。我的操作系统是Windows。

英文:

I have a java program (jar) that uses ProcessBuilder to execute a dynamic batch file.

Process process = (new ProcessBuilder(commands)).start();

I have a simple batch file like this, Z --> is a network drive. let say my batch file location is in my_batch.bat.

copy Z:\Download\*.csv D:\Download\

if I execute this batch file from the command prompt, it run successfully. But if I execute using the java program, the program does not work (nothing happen). But if I change the batch file like below, it works. E-> is a regular drive (not a network drive).

copy E:\Download\*.csv D:\Download\

I don't know why this can happen, please help.
My Operating System is Windows.

答案1

得分: 1

我通过在我的批处理文件中添加这行来解决了这个问题。

net use Z: \Computer\shared /user:DOMAIN\username password

并且在流程结束后,在我的批处理文件末尾使用这行。

net use Z: /d

感谢在评论部分帮助我的人。

英文:

I solved it by putting this line in my batch file.

net use Z: \\Computer\shared /user:DOMAIN\username password

and after the process, use this at the end of my batch file.

net use Z: /d

thanks to the man that help me in the comment section.

huangapple
  • 本文由 发表于 2020年10月23日 18:30:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64498347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定