aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes ant and environment classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "ant and environment classpath" Watch "ant and environment classpath" New topic
Author

ant and environment classpath

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
In my ant script I have
<sql
classpath='mysql-connector-java-3.1.6-bin.jar'
>
(other stuff)
</sql>

In my environment variable CLASSPATH, I have ...;...\mysql-connector-java-3.0.7-stable-bin.jar;...

When I run my ant script from within the IDE, everything works fine. When I run it from the command line, I get "java.sql.SQLException: Communication link failure: Bad handshake" (a connection time-out exception).

So we set the classpath to "" in the console window and then ant worked just fine from the command line.

Is the environment classpath passed into ant before you set the classpath in the target?

Should the environment classpath be "overwritten" when you set a classpath in an ant target?
[ April 12, 2007: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Originally posted by Marilyn de Queiroz:
Is the environment classpath passed into ant before you set the classpath in the target?

Should the environment classpath be "overwritten" when you set a classpath in an ant target?

I don't know the exact answer to your question. So I'll share what I do know about classpaths in Ant in case it puts you or someone else closer to the solution.

Ant deals with a couple of classpaths. Ant itself has a classpath. This is used by Ant in finding tasks. It is set by the classpath that gets passed to Java in through the caller to Ant - typically the build shell script.

Tasks within Ant are also passed a classpath such as javac or the sql task in your example. The JavaDoc for the sql task states that if you don't supply a classpath, it uses the system classpath/environment variable.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Thanks, Jeanne. I looked a little farther down in the ant manual for the sql task and found
"The following connects to the database given in url as the sa user using the
org.database.jdbcDriver and executes the SQL statements contained within the file data.sql, with output piped to outputfile.txt, *searching /some/jdbc.jar as well as the system classpath* for the driver class."

So the next question is, is there any way to skip the "system classpath" part?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Marilyn,
One would think the classpath you specified would get priority over the system classpath. I haven't used the sql task, but that's how all the other tasks I've used work.

One other thing to try: Set the absolute path of the mysql driver in the classpath tag where are you calling the sql task. This will help eliminate (or confirm) the possibility that the requested driver is not found and Ant is being "helpful" by defaulting to the system classpath.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Recommendations about classpath from Ant system requirements - CLASSPATH:

  • Do not ever set CLASSPATH. Ant does not need it, it only causes confusion and breaks things.
  • If you ignore the previous rule, do not ever, ever, put quotes in the CLASSPATH, even if there is a space in a directory. This will break Ant, and it is not needed.
  • If you ignore the first rule, do not ever, ever, have a trailing backslash in a CLASSPATH, as it breaks Ant's ability to quote the string. Again, this is not needed for the correct operation of the CLASSPATH environment variable, even if a DOS directory is to be added to the path.
  • You can stop Ant using the CLASSPATH environment variable by setting the -noclasspath option on the command line. This is an easy way to test for classpath-related problems.


  • [ April 13, 2007: Message edited by: Carol Enderlin ]
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9052
        
      12
    One would think the classpath you specified would get priority over the system classpath.

    That's what I expected, but it didn't seem to be the case.

    One other thing to try: Set the absolute path of the mysql driver in the classpath tag where are you calling the sql task. This will help eliminate (or confirm) the possibility that the requested driver is not found and Ant is being "helpful" by defaulting to the system classpath.

    It's an independent target, not called by another target. When I remove the env classpath, ant finds the driver in the jar I pass in with the target. But when I don't mess with the env classpath, I get a connection error.
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9052
        
      12
    Thanks Carol.

    Originally posted by Carol Enderlin:
    Do not ever set CLASSPATH.


    But I use the classpath for other stuff.

    You can stop Ant using the CLASSPATH environment variable by setting the -noclasspath option on the command line. This is an easy way to test for classpath-related problems.

    This is possibly useful information. What I'd really like to do is cause it to use the target classpath first and the environment classpath afterwards.
    Carol Enderlin
    drifter
    Ranch Hand

    Joined: Oct 10, 2000
    Posts: 1364
    Hi Marilyn,

    I agree with the ant recommendation I quoted (i.e., it works for me in our environment) to unset or have ant ingnore your system classpath and get all the classpath information from the build file. this is important because we want our builds to work on anyone's machine with "minimal" setup.

    Obviously this may require some work if you have been depending on your system classpath.

    So in lieu of that, the build.sysclasspath property may be of use to you; it has values of only, ignore, first, and last... see this URL:

    ant system classpath

    I haven't used this, so I don't know that all ant tasks use this property. As it sounds like you have already figured out, the default behavior for task varies.
    [ April 14, 2007: Message edited by: Carol Enderlin ]
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9052
        
      12
    That sounds like just what I'm looking for. I note that it is something new in 1.7. Thanks.
     
    Consider Paul's rocket mass heater.
     
    subject: ant and environment classpath