Java and Dos Shells - very confused ;-(
I have a problem which I think is something to do with the environment created by Java when you use Runtime.getRuntime() and a Domain evirnoment
I use this method to call a DOS command Net Use and pipe it to file using > tofile.txt.
String mappings = "NET USE > " + LocalPath + "unc.txt";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( mappings );
proc.waitFor();
On my home machine this creates a text file detailing all Mapped Drives, however, at work were have a Windows 2003 domain, all I get is a blank file.
To ensure it wasnt just me going mad I modified the code to create a batch file that would do the same thing.
When the batch file was called from my app using RunTime all that is created is a blank file, however if I click the batch file from a Windows environment the file is populated correctly?!?
Has anyone got any ideas why the static shell created by java is isolated from the users Domain environment, and yet is OK from Windows or a home Network?
Failing that, is there another way I can achieve the same results a different way?!!
Thanks in advance,
Coffee_Junkie
Thanks for the reply,
The blog was very interesting and taught me a couple of new tricks. Unfortunately I don't think it applies to my problem directly as I had already tried the CMD /C option without luck, and do get an output.
My file is always created, so the command is executing correctly, what is baffling me is why at work the file is blank.
To better explain the problem I wrote a batch file out side of java and called it test.bat. It included the following two commands
NET USE > C:\Windows\Temp\unc.txt
ECHO FINISHED >> C:\Windows\Temp\unc.txt
When you double click the batch I get the following reset:
New connections will not be remembered.
StatusLocalRemoteNetwork
-
OKG:\\Staff\Finance$ Microsoft Windows Network
OKH:\\Staff\kris$Microsoft Windows Network
OKM:\\Staff\simsMicrosoft Windows Network
Disconnected O:\\student\install$Microsoft Windows Network
OKR: \\staff\Template Microsoft Windows Network
OKS: \\staff\Shared Microsoft Windows Network
Disconnected T:\\student\TemplateMicrosoft Windows Network
OKU: \\staff\users$Microsoft Windows Network
Disconnected W:\\Student\Users$Microsoft Windows Network
The command completed successfully.
FINISHED
However if I call the batch file from Java with
String cmd = LocalPath + "test.bat";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( cmd );
proc.waitFor();
I get the following output:
FINISHED
This I believe proves my code works and that I haven't fallen pray to RunTime issues discussed in the document (I don't think).
Can anyone point me in another direction as to how to ascertain the current drive mappings assigned to a user in a Windows 2003 Domain environment from within a java application.
Would be eternally grateful for a bit more help,
Coffee_Junkie
Try without using ">". It will give you grief.
public static void main(String[] args) {
String cmd = "net use";
Runtime rt = Runtime.getRuntime();
Process proc = null;
Process proc = rt.exec(cmd);
Scanner inx = new Scanner(proc.getInputStream());
PrintWriter outx = null;
outx = new PrintWriter(new FileWriter(new File("unc.txt"), true), true);
String str;
while((str = inx.nextLine()) != null) {
outx.println(str);
}
}