Javac. Is there any shortcut to specify classpath in command line? - javac

THis is the usual way of compiling using javac
javac .;C:Users\xxx\ . . ; another jar; another jar . . . ; MyTest.java
Is there any way to put the list of references in an external xml, the same way Eclipse does .classpath , and refer to this file rather than list all the external jars in -classpath cmd?

You can use a script instead of retyping this command line each time. Another option is the CLASSPATH environment variable but you'll need to be careful not to set it globally (the doc specifically recommends using -cp instead of the environment variable).
You could write your own launcher that takes an XML file, converts it into a classpath string, and then launches java with the correct classpath argument.

Related

how do i run command-line ant with stuff in the classpath?

In eclipse, I can tell my external ant tool to run with stuff in the classpath.
If i want to run ant from the command line, how would i do that?
For argument's sake, the classpath i want to add is c:\some\folder\here\hooray.jar
Use the -lib argument. From the Ant docs on this page:
Additional directories to be searched may be added by using the -lib option. The -lib option specifies a search path. Any jars or classes in the directories of the path will be added to Ant's classloader.

How can I run ANT from a batch file without having an Environment Variable set?

I want to include ANT, the JavaSDK and FlexSDK into my project directory. I need people in my company to be able to compile from source code.
I have a build.bat file which starts with :
ant blah/blah/blah
But what if I want to run ANT directly without the system environment variable? If I remove these from the system ant is not recognised.
I am a real newbie here, but I want to do something like this:
start "${basedir}\libs\ant\bin" ant -lib ${basedir}/libs/ant/lib/flexTasks.jar
pause
This is so that other people dont need to install a whole load of software... IT would all be contained in the folder.
Just set those environment variables in the same batchfile, i.e. something like :
set ANT_HOME=C:\ant182
set ANT_ARGS=-lib C:\antxtralibs
set JAVA_HOME=C:\jdk160_30
set PATH=%PATH%;%JAVA_HOME%\bin;%ANT_HOME%\bin
:: default
ant -f %1
:: debug
:: ant -debug -f %1

Ant build tool installation not working properly!

i have downloaded the Ant distribution from the Ant web site.
apache-ant-1.7.0
and set environment variable specifying the location of the Ant installationas
ANT_HOME = C:\portal\apache-ant-1.7.0
PATH=;%ANT_HOME%\bin
But ant is not getting properly installed
Better use batch or shell scripts to start your ant files, f.e. =
windows
set JAVA_HOME=C:\java\jdk\1.6.0_xx
set ANT_HOME=C:\ant
set ANT_ARGS=-lib C:\ant_xtralibs;C:\ant_testlibs
set PATH=%PATH%;%JAVA_HOME%\bin;%ANT_HOME%\bin;C:\cvsnt
:: default
call ant -f %1
:: debug
::call ant -debug -f %1
...
unix - don't forget the quotationmarks on the ANT_ARGS line !
...
ANT_ARGS="-lib /usr/local/ant_xtralibs:/usr/local/ant_testlibs"
export ANT_ARGS
...
Some advantages of starting ant like that :
ANT_ARGS is a special environment variable. Its content is automatically
added to the invocation of ant.
you may use your own ant settings on a machine where you have no admin rights
using a separate folder for your ant addon libs and load via -lib option
keeps your ant installation clean and avoids polluting the %ANT_HOME%/lib folder
Move the ';' to the end. Be sure that java is also correct installed (variable for classpath, etc.)
Depending on you verson of windows (win2k & win XP), you might need to reboot your computer to make your PATH modifications taken into account.

ANT global property file in Intellij IDEA?

How to set a global property file so that contain the defined properties are getting resolved during my ANT build in Intelliji IDEA?
In Eclipse I can do this via the "Properties" tab in the "Window/Preferences/Ant/Runtime" config.
How can I set this in Intellij IDEA 9 or 10 ?
IDEA allows you to specify custom properties for the build script, but not via file:
Use this tab to specify the runtime properties that should be passed to the build script. These properties are equivalent to the ones defined after the -D option of the command line. A property is defined as a pair "name-value". The value can be hard-coded, or dynamically evaluated at runtime from a macro.
If you want to use a file, then you need to reference properties file from the build.xml:
Alternatively you can execute the Ant build script on the command shell (View > Tool Windows > Terminal). The command ant can load specific property files, e.g.: <property file="./resources/build.properties" />.

How do I change the JAVA_HOME for ant?

I'm doing java work on a class server where I don't have root. Whenever I try to compile using ant, it points to the wrong directory (/usr/tomcat instead of /usr/tomcat/jre ).
One of the things we were told to do when setting up our user accounts was to add export JAVA_HOME=/usr/tomcat/jre to the .bashrc file. I don't know if that was supposed to take care of the problem but it doesn't seem to.
So, how can I change the JAVA_HOME property for ant but only for when I run ant?
EDIT:
echo $JAVA_HOME points to /usr/tomcat/jre
echo $JAVA_HOME\bin points to /usr/tomcat/jrebin
The problem is when I normally run ant I get this error:
Unable to locate tools.jar. Expected to find it in /usr/tomcat/lib/tools.jar
Buildfile: build.xml
compile:
[javac] Compiling 1 source file to /home/ejm244/build/classes
BUILD FAILED
/home/ejm244/build.xml:9: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
Total time: 0 seconds
You could create your own script for running ant, e.g. named ant.sh like:
#!/bin/sh
JAVA_HOME=</path/to/jdk>; export JAVA_HOME
ant $#
and then run your script.
$ chmod 755 ant.sh
$./ant.sh clean compile
or whatever ant target you wish to run
JAVA_HOME should point at where the JDK is installed not not a JRE.
So, if you type ls $JAVA_HOME what do you see?
if you do ls $JAVA_HOME/bin/ do you see javac?
If the first doesn't work then you don't have JAVA_HOME pointing at the right directory.
If the second doesn't work then you need to point JAVA_HOME at a JDK instead of a JRE.
Looking at the shell script for invoking ant, it is possible that the value for $JAVA_HOME set for your shell in .bashrc can be overridden in the files /etc/ant.conf, $HOME/.ant/ant.conf, and $HOME/.antrc. If you execute bash -x <path to ant script> it will emit debugging information which should help you track down where $JAVA_HOME is being overridden.
On my Windows 7 machine setting:
JAVA_HOME="C:\Program Files\Java\jdk1.6.0_18"
didn't work. But setting:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_18
worked.
There are 2 ways of changing the compiler:
export JAVA_HOME=/path/to/jdk before you start Ant.
Set <javac exectuable="/path/to/javac">
Another option would be to add a respective tools.jar to the classpath, but this is usually used if Ant is started from another tools like Maven.
For more details on these (or other) options of changing Java Compiler in Ant, see this article for example.
Set the env var:
JAVACMD - full path of the Java executable. Use this to invoke a different JVM than JAVA_HOME/bin/java(.exe).
Reference: http://ant.apache.org/manual/running.html
Though the environment variable JAVA_HOME set correctly, the ant may use the configured JRE within the each build.xml or any build files.
To check what version of the JRE the ant is using, right click on the build file -> select the build ant which displays the details about the tasks to choose etc, select the JRE which you want to use.
Its advisable to use the project level settings or just at the workspace level.
JAVA_HOME needs to point to a JDK home if you're trying to compile code. Check to see if '/usr/tomcat/jre/bin/javac' exists. I doubt it does.
If you don't have a JDK, then you can work around it by getting the ECJ (eclipse compiler) library, dropping it into '~/.ant/lib' and adding a system property to the command-line to use that compiler - check the Ant manual for details.
http://ant.apache.org/
Set the JRE in the project (project properties -> Java Build Path-> Libraries, typically last entry), or global default in preferences (Java->Installed JREs) to a JDK, not a JRE.
When using Bash just try this:
$ export JAVA_HOME=/usr/tomcat/jre
When running ant from the shell, you don't need to export JAVA_HOME first, which would set that variable for your current shell and all future commands, instead use
user#host:~# JAVA_HOME=/path/to/jdk ant targets
You will need to change JAVA_HOME path to the Java SDK directory instead of the Java RE directory. In Windows you can do this using the set command in a command prompt.
e.g.
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_14"
try with this:
/usr/sbin/update-alternatives --config java
java_home always points to the jdk, the compiler that gave you the classes,
and the jre is thw way that your browser or whatever will the compiled classes so it must have matching between jdk and jre in the version.

Resources