Running Batch File Using Java ProcessBuilder does not recognized Network drive

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 answer

  • answered 2020-10-24 05:18 esthrim

    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.