I'm using IntelliJ IDEA with Ant on Windows 10 to compile & run my project using build.xml
All my files (even build.xml) are coded in UTF-8
I'm using UTF-8 in XML specification & javac command
<?xml version="1.0" encoding="UTF-8"?>
<javac target="1.8" srcdir="${test}" destdir="${build}" encoding="UTF-8" includeantruntime="false">
I have set UTF-8 as default encoding in IDE
Still not working. It writes diamonds with question marks when there are some diacritics on output.
May it be something related to that I'm making JAR file which is then executed?
The solution is to add jvm argument within build.xml file.
Example:
<target name="execute" description="Launch an app" depends="jar">
<java jar="${dist}/${jarname}" dir="${dist}" fork="true">
<jvmarg value="-Dfile.encoding=UTF-8"/>
</java>
</target>
Related
I would like Ant 1.9 to be able to compile servlet classes to the parent src directory, but the file system has the files in packages. No, the packages are not declared in the servlet files.
The code is deployed with all the servlets in the same directory. I supposed I can create an ant copy command to do this, but I would prefer there be an ant javac solution.
Manually this would be 'javac -d .. *.java' after changing to each subdirectory, not including the files already in the default package. This is highly irregular, but I cannot change the how the packages are defined nor can I change the urls from which the code is executed from.
<target name="compileServlet" description="Compiles Java source files.">
<javac srcdir="${servlets.dir}" destdir="$(servlets.dir}" debug="true" encoding="ISO-8859-1" source="1.7" target="1.7" failonerror="true">
<classpath path="${env.CLASSPATH}" />
<include name="**/*.java"/>
</javac>
</target>
Right now when I run this ant build.xml, no class files are generated. Any thoughts how I can solve this issue?
If you have source files in multiple root directories and want to compile them all into a single root directory, use the <src> element instead of the srcdir attribute.
Here is the example shown in the documentation:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>
I have written the Ant script pasted below. It uses the javac task. The Apache documentation states that scrdir parameter is used to point to the source (.java) files. But it also explains that the javac task can use patternsets.
I believe there may be a bug in the javac task or at least, perhaps a poor implementation of the javac Ant task.
In my code, the compile target fails because the source files cannot be found. Can anyone please explain why?
Background:
Directory structure of my project is as follows:
My ant scripts are in the ant directory.
Source files are inside wm-adapter/src
<?xml version="1.0" encoding="UTF-8"?>
<project name="wm-adapter" default="dev" basedir=".">
<property file="Build.properties"/>
<property name="build.compiler" value="javac1.5"/>
<target name="compile" depends="prepare">
<!-- Define required FileSets and PatternSets... -->
<patternset id="patternset.wm.adapter.sources">
<include name="${source.dir}/*.java"/>
<exclude name=""/>
</patternset>
<fileset id="fileset.wm.adapter.sources" dir="${proj.dir}">
<patternset refid="patternset.wm.adapter.sources"/>
</fileset>
<!-- Compile... -->
<javac srcdir="${basedir}"
destdir="${classes.dir}"
classpath="${compile.classpath}"
debug="${javac.debug}"
verbose="true"
fork="true"
source="1.5"
target="1.5">
<patternset refid="patternset.wm.adapter.sources"/>
</javac>
<echo message=" Compilation completed successfully!"/>
</target>
Here is a snippet from the properties file:
# DEFINE DIRECTORY STRUCTURE...
proj.dir=../wm-adapter
source.dir=${proj.dir}/src
build.dir=${proj.dir}/build
classes.dir=${build.dir}/classes
dist.dir=${proj.dir}/dist
lib.dir=../lib
docs.dir=${proj.dir}/documentation
resources.dir=${proj.dir}/www
config.dir=${proj.dir}/config
# DECLARE ANY OTHER WORKING DIRECTORIES...
tmp.jar.dir=C:\\Temp
tmp.app.dir=${tmp.jar.dir}/FATCA_WM_Adapter
# Name the jar file...
jar.file=FATCA_WM_Adapter.jar
# IMPORT EXTERNAL JARS INTO OUR CLASSPATH...
compile.classpath=${g11nutils.jar};${javax.ejb_3.0.jar};${middleware_UTILS.jar};${wmbrokerclient.jar};${classes.dir}
compile.classpath=${classes.dir};${lib.dir}
basedir has been defined as '.'
I know how to make it work: The target will compile if:
I make changes to the patternset as shown below:
<patternset id="patternset.wm.adapter.sources">
<include name="src/*.java"/>
<exclude name=""/>
</patternset>
Within the javac task I ammend srcdir to:
srcdir="${proj.dir}"
But this is a fix! Why do my original settings not work?
basedir here is equal to your 'ant' subdir, which is probably why it doesn't work. Perhaps you want <project basedir=".."> at the top.
I have a project I build using ant and eclipse which I'd like to have be compatible with Java 8.
When I try running the jar using Java 8, I get the following error:
$ /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar hypnos.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError:
org/joshuad/musicplayer/MusicPlayerUI has been compiled by a more recent version of
the Java Runtime (class file version 53.0), this version of the Java Runtime only
recognizes class file versions up to 52.0
So it appears that it's compiling using Oracle's Java 9 (which is installed on my system) but I can't find the place in eclipse where this is designated. Everything I see seems to suggest that we're using java 8 (screenshots below).
I feel like I must be missing something, but I can't figure out where. What do I have to do to get this jar to be compatible with Java 8 / class file version 52.0?
Project > Properties > Java Build Path
Project > Properties > Java Compiler
Window > Preferences > Compiler
Right Click Ant Build File > Run As > External Tools Configuration
Ant Build File
<project name="Hypnos Music Player" default="compile" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="jarFile" location="hypnos.jar"/>
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
<pathelement location="${jarFile}" />
</path>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source">
<javac fork="yes" includeantruntime="false" srcdir="${src}" destdir="${build}">
<classpath refid="class.path" />
</javac>
</target>
<target name="jar" depends="compile" description="Create a jar.">
<jar destfile="${jarFile}" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="org.joshuad.musicplayer.MusicPlayerUI" />
<attribute name="Class-Path" value="lib/commons-cli-1.4.jar lib/jaad-0.8.4-nospi.jar
lib/jaudiotagger-2.2.6-SNAPSHOT.jar lib/jflac-1.2.jar
lib/jl1.0.1.jar lib/jogg-0.0.7.jar lib/jorbis-0.0.15.jar
lib/vorbisspi1.0.3.jar" />
</manifest>
</jar>
</target>
</project>
What is your $JAVA_HOME environment variable?
I recently had a similar problem and it turns out that Ant seems to ignore many settings provided by Eclipse and only uses $JAVA_HOME. I discovered this by placing the following in build.xml:
<target name="jdk_version_validation">
<echo message="Java Version: ${java.version}" />
<echo message="Java home: ${java.home}" />
<echo message="JAVA_HOME: ${env.JAVA_HOME}" />
<echo message="JAVACMD: ${env.JAVACMD}" />
</target>
Which got me this:
jdk_version_validation:
[echo] Java Version: 1.7.0_80
[echo] Java home: C:\Program Files\Java\jre7
[echo] JAVA_HOME: C:\Program Files\Java\jdk1.8.0_131\
[echo] JAVACMD: C:\Program Files\Java\jdk1.7.0_80\bin
I found that the only sure way to predict which version Ant will produce is by brute-forcing the following in build.xml:
<property name="ant.build.javac.source" value="1.7"/>
<property name="ant.build.javac.target" value="1.7"/>
So, my recommendation to you: Try placing in your build.xml the following:
<property name="ant.build.javac.source" value="1.8"/>
<property name="ant.build.javac.target" value="1.8"/>
Update, for the benefit of those with a variation of the problem described in the OP, to eliminate any possibility of "Eclipse fooling Ant":
Go to Window > Preferences > Java > Installed JREs and try leaving only the target JRE, making sure it is the one inside the JDK folder (not the sibling one directly under the Java folder).
Go to Project > Properties > Java Compiler and check Enable project specific settings, making sure that the Compiler compliance level is set to the target JRE java version.
In addition to what datv said above, I'll add from experience:
Ant's javac, java, and junit tasks have a jvm attribute that determines how to run the JDK/JRE, if fork="true". Unfortunately, it only sets the raw launcher, and doesn't change all the other settings. When I set the jvm attribute to the full path of a Java 8 JRE, turning on ant's -d switch shows that it is being used as the executable, but followed by
'-Djava.specification.version=9'
'-Dsun.boot.library.path=C:\Java\x64\jdk-9.0.1\bin'
'-Djava.home=C:\Java\x64\jdk-9.0.1'
'-Djava.runtime.version=9.0.1+11'
'-Djava.version=9.0.1'
'-Djava.library.path=...;C:\Java\x64\jdk-9.0.1\bin;.....
'-Djava.vm.version=9.0.1+11'
'-Djava.class.version=53.0'
all of which take precedence.
It turns out that Ant cares more about the JAVACMD environment variable when it starts up. In the situation above, our build framework had set JAVACMD to C:\Java\x64\jdk-9.0.1\bin\java, and the individual jvm= and executable= attributes on specific tasks were... not "ignored" really, merely pointless and misleading.
Forewarning: I'm not very bright so the question may seem extremely idiotic.
I need to make an ant file that will compile my source files to a target directory.
In addition, it needs to test all the JUnit files in my project.
However, I understand next to nothing in a build.xml.
At the moment I have my project folder in eclipse contain two separate package folders that hold all my java files.
Would the following be along the right path?
<project name="PersonPersistence" default="compile" basedir=".">
<target name="compile" description="Compiles java source files.">
<mkdir dir="target"/>
<javac srcdir="src" destdir="target"/>
</target>
<target name="test" depends="compile" description="Tests JUnit files.>
<java jar="src/packOne/Test1.java"/>
</target>
</project>
Or do I need to do something else for the "test" portion? Such as putting all my test files into a jar file?
I need to use some java library in MATLAB. I did use it in MATLAB (Unix version), but now I have to do it on MATLAB (Win64), as well. As far as I know, this is a project developed in unix. I did simply compile it using
ant
command in Ubuntu.
Since I'm a beginner to java compiling, I thought of installing ant on Windows and running
c:\java\ant\bin\ant
command in the path. However when I do this, it says that:
build.xml:22: Javadoc failed: java.io.IOException:
Cannot run program "javadoc.exe": CreateProcess error=2
Here is the build.xml file:
<project default="all">
<target name="all" depends="doc,jar" />
<target name="compile">
<mkdir dir="build/classes" />
<mkdir dir="build/examples" />
<javac includeantruntime="false" debug="on" srcdir="src/main/java"
destdir="build/classes" target="1.5" />
<javac includeantruntime="false" srcdir="src/main/example/"
classpath="build/classes" destdir="build/examples" />
</target>
<target name="jar" depends="compile">
<jar destfile="dist/java_websocket.jar">
<fileset dir="build/classes" includes="**/*.class" />
</jar>
</target>
<target name="doc">
<delete dir="doc" />
<javadoc sourcepath="src/main/java" destdir="doc" />
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>
When I try to search about this issue, I've come up with so many answers that are about Java Eclipse compilations, which I'm a total stranger to.
If you use maven to compile these projects, thats fine. I just need some guidance.
Here is the github link of the java project that I'm trying to compile (on Win64), if you would like to see any further information: https://github.com/TooTallNate/Java-WebSocket
And here is the build instructions (I'm guessing for Unix): https://github.com/TooTallNate/Java-WebSocket/blob/master/README.markdown
Note that I sadly use Windows 8 (x64) and I currently have jdk1.7.0_21 installed on the path "C:\Program Files\Java\jdk1.7.0_21" which is also the system variable JAVA_HOME. ANT_HOME variable is "c:\java\ant"
Please take a moment from your precious time and help me to find a solution, if you have any knowledge about this issue. Any help will be greatly appreciated.
Best regards.
If it compiles on ubuntu with just ant, there is no need for either maven or eclipse. The problem is likely that the PATH variable does not include the jdk. Look into c:\Programs\Java and find the javadoc.exe. Its directory must go into PATH. To set the dir in PATH, go to (roughly, have no windows around) computer / properties / advanced / environment variables / system and find the PATH variable. Append the directory you found by using semicolon as the separator.