Chance
Chance

Reputation: 988

Opening Files with Java while Working in Cygwin

I am running Cygwin on a Windows 7 machine, and using script files to execute Java programs in batch. My problem is this: I try to pass in a Cygwin / Linux path to a file, via the command line, and Java converts all of the forward slashes to backslashes.

For instance:

java program $scratchname/path_to_folder/ filename_$i.txt

Within Java, I take the directory and add the file name to open the file, which usually works with no issues as long as I'm using a Windows command line. However, in Cygwin Java converts this to:

home\scratch\path_to_folder

which Cygwin doesn't like.

I don't think this is a simple matter of replacing the backslashes with forward slashes, because Java seems to default to the Windows path conventions when I try to open the file. I'm guessing this is because Cygwin is pointed to the Windows installation of the JVM.

How can I force Java to use Cygwin / Linux path name conventions on a Windows system?

Upvotes: 3

Views: 1264

Answers (1)

Yaakov
Yaakov

Reputation: 1703

Java is a Windows program, and as such, only understands Windows paths; launching it from a Cygwin shell can't change that. You can use cygpath to convert paths back and forth.

Reference link: https://cygwin.com/cygwin-ug-net/using-effectively.html

Example case:

  java -jar path/to/your-1.0.jar "$(cygpath -aw /home/YOUR_USER/path/to/file.txt)"

Options:

  • a provides the absolute path
  • w uses the Windows format

Upvotes: 5

Related Questions