Here is how I have set this up to work:

Project Antproject = new Project();

boolean t = true;
Path path = null;
AntClassLoader ACL = new AntClassLoader(Antproject, path, t); 
//this causes ant to use the system path classpath since I passed in a
"true" value. To append //items, use something besides path = null.
Antproject.setUserProperty("ant.file", someFile.getAbsolutePath());
Antproject.fireBuildStarted();
Antproject.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
Helper.parse(antproject, someFile);
Antproject.executeTarget("package");
Antproject.fireBuildFinished(null);

This is not the complete source, but I did have to add some methods,
etc. You will probably want to add some stuff to the classpath to
include all the jar files in ant/lib directory. I also added the
java_home/lib/tools.jar to the classpath on my system.

I am going to start a blog with some of this info, because I also had a
heck  of a time capturing the javac tasks output when adding a logger to
the build, as well as some other issues. 


I was running this java program using a <java> task in a separate ant
file, btw. I also intend to fire a version from a perl script.


-----Original Message-----
From: Ashley Williams [mailto:ashpub...@mac.com] 
Sent: Sunday, May 17, 2009 9:00 AM
To: Ant Users List
Subject: Re: Java in JDK but ANT can't find it!

Hi David,

I never meant that you should modify the ant code.
Try launching your app with the fully qualified
path to the jdk java eg:

 > /path/to/jdk/bin/java MyTestApp

So in other words don't rely on your PATH environment variable. It  
could be
also that the bootclasspath has been overridden so that tools.jar has  
been
taken out. Are you on the mac, in which case you don't have tools.jar?

Try the same experiment by stealing the very last line in the ant  
launcher
script and hardcoding the values that you know you need to use. On my
installation of ant I would start with this line:

ant_exec_command="exec \"$JAVACMD\" $ANT_OPTS -classpath  
\"$LOCALCLASSPATH\" -Dant.home=\"$ANT_HOME\" -Dant.library.dir= 
\"$ANT_LIB\" $ant_sys_opts org.apache.tools.ant.launch.Launcher  
$ANT_ARGS -cp \"$CLASSPATH\" $ant_exec_args"

I say this again, the idea behind this is that you need to assume that  
your
environment variables and shell scripts can't be trusted and therefore
bipass them.

I mentioned this before, but remove any compiler fork option so that  
the task
is being executed in the same vm. Then if it behaves differently in a  
forked
vm, you know that the vm environment isn't being inherited properly and
that's where you need to look.

You could try removing all java installations from your machine and then
reinstalling just the jdk that you need. I know for example that windows
gets up to tricks such as adding a java.exe in the windows system  
directory
that is always first in the PATH that you have to remember to manually
delete.

Sorry you are having all these frustrating problems, good luck.


On 16 May 2009, at 20:25, David Nemer wrote:

> Just a remark, when I run the application and /opt/jdk1.6.0_06/lib/ 
> tools.jar
> to the classpath. It gives me a different error message:  "Compile  
> failed;
> see the compiler error output for details." there is no error msg  
> and it
> still says BUILD SUCCESSFUL
>
>
> --
> David Nemer
> Sent from Kaiserslautern, RP, Germany
>
> On Sat, May 16, 2009 at 9:11 PM, David Nemer <davidne...@gmail.com>  
> wrote:
>
>> Hello Michael,
>>
>> here is my code on how I'm invoking Ant:
>>
>>            Project p = new Project();
>>            p.setUserProperty("ant.file",  
>> buildFile.getAbsolutePath());
>>            DefaultLogger consoleLogger = new DefaultLogger();
>>            consoleLogger.setErrorPrintStream(System.err);
>>            consoleLogger.setOutputPrintStream(System.out);
>>            consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
>>            p.addBuildListener(consoleLogger);
>>            p.fireBuildStarted();
>>            p.init();
>>            ProjectHelper helper = ProjectHelper.getProjectHelper();
>>            p.addReference("ant.projectHelper", helper);
>>            helper.parse(p, buildFile);
>>            p.executeTarget(p.getDefaultTarget());
>>
>> Michael, I read somewhere that when you invoke ANT in java, it runs  
>> in the
>> same JVM, and forking the ANT process might solve it. Do you have  
>> any idea
>> how would I code this?? Make Ant ran in another JVM?
>>
>>
>> --
>> David Nemer
>> Sent from Kaiserslautern, RP, Germany
>>
>> On Sat, May 16, 2009 at 9:03 PM, Michael Ludwig <mil...@gmx.de>  
>> wrote:
>>
>>> David Nemer schrieb am 16.05.2009 um 20:51:31 (+0200):
>>>
>>>> ne...@kubrick:~$ java -jar
>>> /home/nemer/apache-ant-1.7.1/lib/ant-launcher.jar
>>>> -version
>>>> Apache Ant version 1.7.1 compiled on June 27 2008
>>>>
>>>> ne...@kubrick:~$ /opt/jdk1.6.0_06/bin/java -jar
>>>> /home/nemer/apache-ant-1.7.1/lib/ant-launcher.jar -version
>>>> Apache Ant version 1.7.1 compiled on June 27 2008
>>>>
>>>> so just running JAVA worked as well as running /opt/jdk1.6.0_06/ 
>>>> bin/java
>>>
>>> So tools.jar is found alright. It's an environmental problem,  
>>> either in
>>> the ant shell script (has that been tampered with?) or, more  
>>> likely, the
>>> shell script you're using to launch it.
>>>
>>> Michael Ludwig
>>>
>>>
---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
>>> For additional commands, e-mail: user-h...@ant.apache.org
>>>
>>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to