File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is there a way to detect what platform(unix or windows) my Java code is running on? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a way to detect what platform(unix or windows) my Java code is running on? " Watch "Is there a way to detect what platform(unix or windows) my Java code is running on? " New topic
Author

Is there a way to detect what platform(unix or windows) my Java code is running on?

Nancy Zhang
Greenhorn

Joined: Jan 07, 2005
Posts: 25
so that my code could process current directory correctly, e.g. ".\" for windows usage, and "./" for unix usage.
Thanks.
[ February 19, 2005: Message edited by: Nancy Zhang ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You can use System.getPropereties("os.name") to get the operating system's name. But the better way to do what you are trying to do is to use the static separator or separatorChar variables of the File class to determine what separator the OS uses. You can then construct a path:

Look at the API Docs for the File class for more information.

[edit note: My API link previously was linking to the J2EE API docs rather then the J2SE docs; sorry for any confusion]
[ February 19, 2005: Message edited by: Mark Vedder ]
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Originally posted by Nancy Zhang:
so that my code could process current directory correctly, e.g. ".\" for windows usage, and "./" for unix usage.
Thanks.

[ February 19, 2005: Message edited by: Nancy Zhang ]


It shouldn't matter, as already said, just use something like



Ands then when you need to refer to it, just replace any references to a slash for either OS and refer to the variable. This is better in my opinion.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you need to concat directory names, you can also use the appropriate File constructor, so that you don't have to care about the seperator at all.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to detect what platform(unix or windows) my Java code is running on?
 
Similar Threads
How to capture unix cpu and memory usage....
Using CachedRowSet
remote debug hot code replace
Firing of Character method of Defualt Handler
using Runtime.exec() and the process exitValue = 1