• Post Reply Bookmark Topic Watch Topic
  • New Topic

Windows system root  RSS feed

 
Charles Hasegawa
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get the %systemroot% property?
We can assume that the user has given us/java permission to access the file system etc.

I've tried :


and then getting the input and output streams and to push commands to, with no luck.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should work. I will bet you are not putting a line separator on the end of the command. Remember, line separators are not platform independent, and the UNIX one ('\n') does not work on Windows (which expects "\n\r").
Don't hard code the line separator. Either use System.getProperties() to get it or use a PrintWriter or BufferedWriter which provide methods to place a line separator in the stream.
Don't forget to flush the stream after the line separator.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well - using "cmd" the system-indepence is already lost.

But what is the %systemroot% property?
The filesystem-root?
 
Roshan Lal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Hasegawa:
Is there a way to get the %systemroot% property?
We can assume that the user has given us/java permission to access the file system etc.

I've tried :


and then getting the input and output streams and to push commands to, with no luck.


try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /k echo %systemroot%");
InputStream is = p.getInputStream();
byte[] b = new byte[100];
is.read(b);
String s = new String(b);
System.out.println(s.trim());

} catch (Exception e) {
e.printStackTrace();
}

I just tried the above code and it worked for me.
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get any environment variable value from any Windows OS flavor with this utily class:

And here is an example on how to use it (very simple indeed):
 
Charles Hasegawa
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, lots of good help.

Originally posted by Stefan Wagner:
well - using "cmd" the system-indepence is already lost.
But what is the %systemroot% property?
The filesystem-root?


We weren't trying for system independence with this - we know the client will be on a Win OS but it could be 98, 2k, XP, etc. This all came about because we have to download a dll to run a signature pad, and so we need to know the client's system32 directory location (which can be in the "windows" folder or "winnt" folder and can be on any drive, etc).

On WinOS, open a command prompt and tyep "ECHO %systemroot%" and you should get something like "c:\windows" (or whatever).
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I humbly suggest that you don't do it through exec ("..cmd...")
but that you use JNI to call SHGetFolderPath()? If I understand
correctly you already use JNI for the DLL, therefore it's no sweat.
I also believe it is, well, slightly cleaner than calling cmd.

Best regards,
Petr
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well - I don't have a windows to type in 'echo %systemroot%', but I got a hint:
If you're allowed to use java-1.5,

should work. But probably you're not allowed.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!