The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java, Bash, Cygwin, ...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Java, Bash, Cygwin, ... Posted: Mar 10, 2008 5:06 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Java, Bash, Cygwin, ...
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

I haven't used the A, B, C, ... style title for a while. Today's post calls attention to all distributors of Java products that provides a Windows batch file and a Unix shell script to run their product:

YOUR SHELL SCRIPT DOESN'T WORK FOR CYGWIN

The problems usually stem from the assumption that a shell script works in the Unix like environment where paths are separated by a ":". Well, for Java under Cygwin, this assumption is no longer valid because the Windows version of the JDK commands such as java or javac expect the CLASSPATH environment variable and the arguments to the -classpath and -bootclassapth command line options to be in the Windows format—with a ";"-separator.

As an example, here is the java shell script shipped with the CICE/ARM closures prototype:

#!/bin/sh
CICEARM_HOME=`dirname $0`/..
java -Xbootclasspath/p:$CICEARM_HOME/lib/rt.jar "$@"

There are two problems with this script. First, it's self recursive, so you cannot put the directory that contains this script into your PATH, or it will call itself in an infinite loop. That's easily fixed by resolving the java call not through the PATH but through JAVA_HOME:

#!/bin/sh
CICEARM_HOME=`dirname $0`/..
"${JAVA_HOME}/bin/java" -Xbootclasspath/p:$CICEARM_HOME/lib/rt.jar "$@"

That brings us to the second problem: the mis-interaction between the Unix style directory names and path separators that bash uses and the JDK commands. If I install the CICE/ARM prototype in the /opt/cicearm-b05 directory, and add /opt/cicearm-b05/bin to my PATH, the above script will expand a simple java Foo command into:

C:\Program Files\Java\jdk1.6.0_10/bin/java -Xbootclasspath/p:/opt/cicearm-b05/bin/../lib/rt.jar Foo

Notice here that we are passing a Cygwin style path /opt/cicearm-b05/bin/../lib/rt.jar to the java command, which will interpret the path as a Windows style path. Most of the time, this will lead to the wrong result.

Fortunately, Cygwin provides a command cygpath that can be used to convert the Cygwin style path into a Windows style path. With the help of this command, and a little bit of uname magic to detect if the bash script is running in Cygwin, we can make the script run properly in Cygwin:

#!/bin/sh
CICEARM_HOME=`dirname $0`/..

# detect Cygwin
cygwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true;
esac

BCP=${CICEARM_HOME}/lib/rt.jar

if $cygwin; then
  BCP=`cygpath --path --windows $BCP`
fi

"${JAVA_HOME}/bin/java" -Xbootclasspath/p:${BCP} "$@"

I know there are not a whole lot vocal Cygwin users out there. Most users would just fix their bash scripts for Cygwin quietly and move on. But for projects still in development, doing the same modification for every snapshot release becomes tiresome very quickly. That's why I usually send my modifications back to the project. And sometimes they even took it.

Read: Java, Bash, Cygwin, ...

Topic: RIA Weekly #008 - Appcelerator, MIX08, SXSW, DRM in Flash from PeopleOverProcess.com Previous Topic   Next Topic Topic: Book Excerpt: Java EE 5 Development Using GlassFish Application Server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use