Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nancy Zhang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic