简体   繁体   中英

File separator Java. Is it determimed at compile or runtime?

I have the task to convert a code, written to be run under Windows, to also run under Linux.

The first issue I am trying to fix is that the File.separator is not used. Instead there are bunch of hardcoded Srings using the windows file separator \\ .

I am running the java code under Linux environment via VirtualMachine, which is bit slow.

In order to make my life easier I just compiled my code ( it takes about 1h to compile the entire code) under Windows, and pasted the class files into the Linux Environment.

So my question is, does File.separator get determined at runtime or compile time, because I am still facing the same issues with paths?

It is determined at runtime.

In the OpenJDK 8 implementation , the File#separator will be initialized through the the FileSystem returned through DefaultFileSystem#getFileSystem() . This class will be provided by the JRE. It will give you a different value depending on a Windows or Linux installation.

In the OpenJDK 7 implementation , a FileSystem implementation was returned through a native call. The value returned again depended on the local environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM