How to use GCJ with Ant? - java

I'm fairly new to both Apache Ant and GCJ, and I'm having a hard time trying to build with GCJ via Ant.
My app is in Scala, so I need to use GCJ to take .class files as source. No problem compiling .scala to .class with Ant.
First I figured out how to manually compile a .class file to .o (object), this way:
gcj --classpath=(...) -c (somepath)MouseClickListener.class -o (somepath)MouseClickListener.o
I see here that Ant supports GCJ compilation through the javac tag. So I figured this should work:
<target name="gcjCompile" depends="compile">
<mkdir dir="${object.dir}" />
<javac srcdir="${build.dir}"
destdir="${object.dir}"
compiler="gcj"
executable="C:/gcc/gcc-4.3/bin/gcj.exe"
classpathref="gcjProject.classpath">
<include name="**/*.class"/>
</javac>
</target>
But this javac task does nothing and I get no errors. Any clues?
Thanks

It sounds like you want to link your app into a native executable. That means that you've already compiled the source into JVM bytecode (as you've figured out to do by compiling .scala into .class files). You'll need to run the gcj command manually using the <exec> task to compile the bytecode into gcc object code files.
I'd recommend something like this:
<property name="main.class" value="Main" />
<property name="class.dir" value="${basedir}/classes" />
<target name="compile">
<mkdir dir="${class.dir}" />
<javac srcdir="${build.dir}"
destdir="${class.dir}"
compiler="gcj"
executable="C:/gcc/gcc-4.3/bin/gcj.exe"
classpathref="gcjProject.classpath">
<include name="**/*.java"/>
</javac>
</target>
<target name="link" depends="compile">
<mkdir dir="${object.dir"} />
<exec cmd="C:/gcc/gcc-4.3/bin/gcj.exe">
<arg value="-classpath=${object.dir}" />
<arg value="-c" />
<arg value="*.class" />
</exec>
</target>
Keep in mind that you need to define the build.dir and object.dir properties, and you may need to add a depends task before the javac in the compile target (or just recompile from scratch each time). I may have missed a lot of things, you should check the manual pages (for gcj, gcc, and ant) if it doesn't work at first.

Related

Building with Ant in Eclipse - javac not recognizing lambda expression (Java 1.8)

I am trying to build an existing project using Ant in Eclipse. The problem is that javac does not recognize the use of a Lambda expression (error: illegal start of expression) in one of the files, and the build fails during the compile phase of the Ant.
Within Eclipse, I've ensured that the Java Compiler compliance level is set to 1.8 and that Java 8 is in the Java Build Path.
I've also ensured that the my Path, JAVA_HOME, and JRE_HOME all point to my Java 8 directory (in Path it points to the /bin directory).
For giggles, the compile section of my build.xml file is:
<target name="compile" depends="setup">
<javac destdir="${base}/${build.dir}"
srcdir="${base}/${src.dir}"
deprecation="true"
verbose="false"
includeantruntime="false">
<classpath refid="libs" />
</javac>
</target>
I'm not really sure what to do next. I've resorted to restarting Eclipse hoping for magic to happen. Any suggestions or help are welcome! Thanks in advance.
Adding <echo> Java version: ${ant.java.version}</echo> to my build.xml revealed that Ant was still running Java 1.7.
I added the directory location of my Java 8 javac and modified the javac task with the executable and fork attributes to use it:
<property name="javac1.8" location="/path/to/java8/bin/javac" />
<target name="compile" depends="setup">
<javac executable="{$javac1.8}" fork="yes"
destdir="${base}/${build.dir}"
srcdir="${base}/${src.dir}"
deprecation="true"
verbose="false"
includeantruntime="false">
<classpath refid="libs" />
</javac>
</target>
Thanks so much for the comments #Jim Garrison, #wero, and #greg-449. Your powers combined lead me to my answer.

Could not find or load main class Java

Good evening Stack Overflow!
I started learning Java quite a few days ago, but not using an IDE once my computer is a bit slow. So, I decided to use Sublime Text, and compile things using ant on the cmd, since it seems to be the most reasonable thing to do.
Today I started ( at least tried ) to follow along a series of LWJGL tutorials (which are really cool), from ThinMatrix, but I can't manage to solve an error which I get every time I try to compile the 'project'.
This is the structure of my project:
LWJGL
src
com
game
test
MainGameLoop.java
renderEngine
DisplayManager.java
build.xml
And ladies and gentlemen... the build.xml (following Ant's official HelloWorld tutorial):
<project name="LWJGL" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="lib.dir" value="lib"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.game.test.MainGameLoop"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<jvmarg value="-Djava.library.path=lib/natives-win" />
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
Every time I run ant on the command line, inside the root folder of my project, I get this:
[java] Error: Could not find or load main class com.game.test.MainGameLoop
[java] Java Result: 1
I'm struggling to understand what is the root problem of this, I kinda sexually abused Google Search trying to find an answer on Java forums, Blogs, and even here...
I don't usually like populating Stack Overflow with noob questions, but I have to admit I don't know what to do.
Thanks in advance!
Your code depends on libraries. You have successfully added the libraries in the classpath to compile your code, and have thus successfully created an executable jar files containing your classes. But these classes depend on libraries to run. And when running the jar file, you don't secify anywhere that Java should look for classes in libraries in addition to your jar file.
See Generate manifest class-path from <classpath> in Ant for how to add a Class-Path entry to the manifest of your executbale jar file. Beware: the paths of the libraries must be relatie to the path of the jar.
Ant is a bit outdated. If I were you, I'd try using gradle, which has a neat application plugin doing all that for you, and much more.
I finally managed to solve this problem that I was struggling with by the help from a good friend of mine. There are two tricky points:
First, when specifying the "Main-Class" in the manifest section, just use the following format "packageName.className", and there is no need to specify the "build" or "source" folders!
Second, try and zip all the jar files that your "jar" file is going to depend on using "zipgroupfileset"
This image shows how I have commented out the "Class-Path" attribute that I would use for addressing the dependencies of the project, and have replaced it with the "zipgroupfileset". I hope that help you as well.
Small thing that can cause this issue is using uppercase letters in file extension. For example you can't use MainGameLoop.Java, The correct extension should be all lowercase like below.
MainGameLoop.java
Please check that first.

How to make build.xml file to run a program?

Two questions.
How do I make a build.xml file run a Java program? What are the commands?
Is there a way to run a Java program through Ant , without creating the .class files?
1- You can use a java command to run java program and create a custom task like:
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
2- I think no, you need to comile your code before execute it, and it's not a big problem, if you use ANT. Just make a task to do that like:
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
And then you can execute a main method of custom java class, like:
<java classname="com.example.MainClass" depends="compile">
<classpath>
<pathelement path="build/classes"/>
</classpath>
</java>
Look here for a basic tutorial on Ant: https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
As far as I know, it is impossible to run a Java program without creating .class files, as the JVM interprets .class files rather than .java files.
I think you have misunderstood the purpose of Ant. It builds Java applications, and part of this build involves compiling the .java files into .class files for you. Typically this would be so that you can deploy applications onto a server more easily.
To compile .java files using Ant, use the javac command, as in the tutorial:
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>

Classpath, Compile, and Run with Ant?

I'm completely new to Ant and need to add a couple jars to my classpath, compile a couple .java files, and run a junit test, all in Ant. I've been looking at a few online tutorials and manuals, but can't seem to wrap my head around the entire xml writing process.
All the previously-written code resides in a single directory called XXX.
In XXX there are two jars I need to add to my classpath with export CLASSPATH=$CLASSPATH:jar1:jar2, two java files I compile with javac *.java, one of which contains several junit tests that I run with java org.junit.runner.JUnitCore Tests. The build.xml would reside in XXX as well (I believe).
So far I have the following for just compiling, although I think there's a lot missing.
<?xml version="1.0"?>
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<target name="compile">
<!-- Compile .java files -->
<javac srcdir="${src}" destdir="${src}"/>
</target>
</project>
What else do I need to add to compile *.java in the current directory? How can I run the export CLASSPATH command, and finally the junit commend?
I'm not asking for anyone to write my code, but it would be appreciated. If anyone knows a good beginner tutorial for a unix environment, that would be awesome. I'm a total beginner with ant so I'll take what I can get.
Here is a previous question addressing this. And this may work for you:
<project name="EtlAutomation" default="compile" basedir=".">
<property name="src" value="${basedir}"/>
<path id="compile.classpath">
<fileset dir="./">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile" >
<javac destdir="${src}" srcdir="${src}">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<junit>
<classpath refid="compile.classpath" />
<test name="TestExample" />
</junit>
</target>
</project>

Getting CLASSPATH from environment in ant build.xml

How can I get the CLASSPATH from the environment in a build.xml?
I've tried
<property environment="env"/>
<path id="classpath">
<pathelement location="${env.CLASSPATH}"/>
</path>
<target name="compile">
<javac includeantruntime="false">
<src path="${src}"/>
<classpath refid="classpath"/>
</javac>
</target>
I have a feeling this is failing because ${env.CLASSPATH} is a colon-separated list.
How then, can I get my classpath? I was surprised when ant didn't use my environment's CLASSPATH.
EDIT:
I've found a quick solution, but the preferred method is to use a separate properties file like the example here http://www.java2s.com/Code/Java/Ant/Useseparatepropertyfile.htm
Solution is, add
<property name="build.sysclasspath" value="first"/>
to the top of the build.xml
Yes, it's failing because it's a colon-separated list. In general, it's considered a bad idea to specify the class-path externally and use it within Ant. This is because running the same Ant script on different machines may yield different results. Instead, you're better of specifying the class-path from within Ant.

Categories

Resources