• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

how to work java code both on Windows and Unix

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Unix-style paths on both platforms. Java will do the conversion for you on Windows.
 
Rancher
Posts: 5013
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using the classpath is not feasible, factoring system-dependent paths into properties files may be an option.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79655
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file.separator property is copied in the java.io.File class, where it is maybe easier to find.
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic