• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to work java code both on Windows and Unix  RSS feed

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code



here i am getting the _destinationLocation value from .properties file

as C:\xyz this fine for window..
and working fine here
but in UNIX it is failing as
the directory stucture is.. "./xyx"
so my code is breaking
can any one tell how to handle..

or how to diferentiate whether it is unix or window os
 
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Unix-style paths on both platforms. Java will do the conversion for you on Windows.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you have mulitple drives? C: D: etc
On Linux, do all paths start from a single root?
Would that mean that code has to be sensitive to which OS its on?

There are File fields that define path separators that should be used.
And System properties such as user.dir
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kajal mukergi:
this is my code



here i am getting the _destinationLocation value from .properties file

as C:\xyz this fine for window..
and working fine here
but in UNIX it is failing as
the directory stucture is.. "./xyx"
so my code is breaking
can any one tell how to handle..

or how to diferentiate whether it is unix or window os
 
Chandan Ghosh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kajal mukergi:
this is my code



here i am getting the _destinationLocation value from .properties file

as C:\xyz this fine for window..
and working fine here
but in UNIX it is failing as
the directory stucture is.. "./xyx"
so my code is breaking
can any one tell how to handle..

or how to diferentiate whether it is unix or window os


Hi,

Use File.separator instead of hard coded string which will enable your code to run in both env.

for (String file : files) {
File destinationLocation = new File(_destinationLocation);
String _destpath = destinationLocation.getPath();
_destpath += File.separator;

_destpath += file;
}
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best solution for this sort of thing is to not use hard-coded paths for anything, since they are system dependent. The best option is to store the file in a location inside your application's class path and use this.getClass().getResource("/xyz") or this.getClass().getResourceAsStream("/xyz");
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using the classpath is not feasible, factoring system-dependent paths into properties files may be an option.
 
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the "file.separator" system property (instead of hard-coding "\\" or "/") - it is set properly based on the host os. Example:

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file.separator property is copied in the java.io.File class, where it is maybe easier to find.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!