J2EE Tutorial: Exception when building with ant

I am trying to build the converter example on page 48 of the J2EE tutorial using ant. Ant seems to choke on the following line from the build.xml:

<property environment="myenv" />

The output of the "ant converter" command is:

Buildfile: build.xml

init:

BUILD FAILED

/usr/j2sdkee1.3/j2eetutorial/examples/src/build.xml:18: /usr/j2sdkee1.3/j2eetutorial/examples/src/build.xml:18: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Nested Exception

/usr/j2sdkee1.3/j2eetutorial/examples/src/build.xml:18: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Nested Exception

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

at java.lang.String.substring(String.java:1525)

at org.apache.tools.ant.taskdefs.Property.loadEnvironment(Property.java:248)

at org.apache.tools.ant.taskdefs.Property.execute(Property.java:172)

at org.apache.tools.ant.Target.execute(Target.java:153)

at org.apache.tools.ant.Project.runTarget(Project.java:898)

at org.apache.tools.ant.Project.executeTarget(Project.java:536)

at org.apache.tools.ant.Project.executeTargets(Project.java:510)

at org.apache.tools.ant.Main.runBuild(Main.java:421)

at org.apache.tools.ant.Main.main(Main.java:149)

Total time: 1 second

My best guess is that the "myenv" line in the build.xml requires some additional attribute. I'm using ant version 1.3. My OS is Solaris (noted for possible environment-related issues).

Thanks in advance!

Eric Smith

[1640 byte] By [essmith] at [2007-9-26 4:46:19]
# 1
<property name="myenv" value="somevalue" /> is the right syntax.If you have more trouble with yuor build.xmlpost it so we can have a look.Bo
Bnarva at 2007-6-29 18:35:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

The build.xml file contains plenty of <property name=...> tags, but <environment> is another. At least that is how the default build.xml was when I installed the J2EE SDK.

I expected the 1.3 build of ant to recognize that. I'm looking for some sort of documentation that tells me how to use <environment> or that it's not compatible with Sun OS.

essmith at 2007-6-29 18:35:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

I made an error in my last post. It is the tutorial from java.sun.com that has this build file.

But this is how they set up the file:

<property environment="myenv" />

<property name="j2eepath" value="${myenv.J2EE_HOME}/lib/j2ee.jar" />

<property name="webpath" value="${webejb}" />

<property name="connector-lib" value="${myenv.J2EE_HOME}/lib/connector" />

<property name="cloud-lib" value="${myenv.J2EE_HOME}/lib/cloudscape" />

<property name="system-lib" value="${myenv.J2EE_HOME}/lib/system" />

It's as if when ant reaches the environment tag, it should perform some OS-level command (like 'env') to store all environment settings.

essmith at 2007-6-29 18:35:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

No there is no such tag, you must use the form above.

You could allso use some other options but no environment.

Perhaps you have got build.xml from SUN wher they have defined one tag themselfes or is it a new release from ant, anyway I have not heard of that tag.

- Bo

bore1 at 2007-6-29 18:35:29 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

The <property environment="xxx" />

tag exists, Bo. How do I know? For one thing, if you put in a bogus tag, like:<property wagga_wagga="huh" />

You get the error:

Class org.apache.tools.ant.taskdefs.Property doesn't support the "wagga_wagga" attribute

If environment didn't exist, then I would see a message similar to above. How else do I know? Well, you can search google.com for "property environment" and ant, you'll see many other users including the environment attribute.

I've worked around the problem by explicitly setting the property tag in those cases where ${myenv} was used. I still would like to know why this failed.

essmith at 2007-6-29 18:35:29 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
I agree, I allso would like to know. I read the doc for antfor property and there was no environment named there, so the question remains are there a newer ver of ant.I use ant 1.3- Bo
bore1 at 2007-6-29 18:35:29 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

On Windows XP Pro, the line

<property environment="myenv" />

in the init target of build.xml for the j2ee tutorial examples triggers a windows error message:

16 bit MS-DOS Subsystem

=======================

C:\WINDOWS\system32\ntvdm.exe

Error while setting up environment for the application. Choose 'Close' to terminate the application.

<<Close>><<Ignore>>

So it looks as if ant is trying and failing to access the winnt system environment here.

According to Ant Developers Handbook (Sams, October 2002, chapter 4, p. 151), the ant environment property "specifies a prefix, giving access to OS-specific environment variables. This is not supported on all platforms." Until this problem is fixed, it appears that on XP systems anyway, any ant settings in build.xml files that rely on direct access to the system environment will fail. Here is some debug output from the j2ee tutorial examples init target which demonstrates this:

C:\apps\j2sdkee1.3.1\j2eetutorial\examples>ant init

Buildfile: build.xml

init:

[echo] starting init target

[echo] reached marker 1

[echo] reached marker 2

[echo] value of J2EE_HOME is ${myenv.J2EE_HOME}

[echo] finished init target

BUILD SUCCESSFUL

where the task:

<property environment="myenv" />

(which generates the OS error popup noted above) occurs between marker 1 and marker 2. Clearly the value of J2EE_HOME is NOT being successfully read from the system environment here.

zuohan at 2007-6-29 18:35:29 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...