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>
Related
I need open this application-->
Decision tree
I have used netbeans and it has not worked, so I have installed eclipse and it seems that the file recognizes me.
What I do is import that application from Github, until there everything perfect, the problem is that when I give it to run, it does not work.
One problem I get is the following:
Buildfile: C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build.xml
compile:
[javac] Compiling 9 source files to C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build\classes
BUILD FAILED
C:\Users\user1\workspace\Arbolito\java-decision-tree-master\build.xml:19:
C:\Users\user1\workspace\Arbolito\java-decision-tree-master\lib
does not exist.
Total time: 550 milliseconds
I put the line of code in the XML file that gives me the problem
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" debug="on" />
</target>
Does anyone know how to run the program?
Create a lib folder in your project and include SL4J and Junit jar in that folder.
I have create RESTful web service based on the JAX-RS and used Jersey embedded web server. My ant script compiles code successfully while it gives me error ClassNotFoundException when I run my main class. So after doing research I came up with solution & here it goes java build ant file with external jar files . What I did was created a bundled jar file try to execute that & it works perfectly fine. I want to know the reason behind :
why this solution works ?
Why I should combine all jar file ?
Is it similar to war file which we create following J2EE architecture otherwise war will not be extracted by server ( say TOMCAT ) & in my case jar file for Jersey embedded HTTP server?
EDIT:
Here is my ant build.xml file
<property name="lib.dir" value="${user.dir}/lib"/>
<property name="build.dir" value="${user.dir}/build"/>
<property name="build.lib.dir" value="${build.dir}/lib"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="src.dir" value="${user.dir}/src/main/java"/>
<property name="main.class" value="com.assignment.ConsoleServer"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="copy_jars" depends="init" >
<copy todir="${build.lib.dir}" >
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy_jars">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="classpath" includeantruntime="false"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.dir}/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
Here is my folder structure
P.S. I am not java expert so pardon me if this question is stupid.
Why this solution works?
In your particular case, you probably didn't include all of the necessary dependencies in your deployment in your previous. (It is not clear from your question how you were originally doing the deployment.)
Now you have put all of the application and dependent class files, etc into one JAR file, and presumably you are deploying / running that file. It works because now it has everything that it needs to run ... which it didn't before.
Why I should combine all jar file?
In your case I suspect that it was not strictly necessary. There was probably a way to "deploy" all of the dependencies without combining them into a single JAR file.
However, there is one case where a "uber-jar" has advantages. That is when the JAR is intended to be an "executable" JAR, and you want to be able to distribute / install it as a single file. (And executable JAR
file can refer to external JARs, etc, but the way that you have to do
it is "fragile".)
Is it similar to war file ... ?
Sort of, though a WAR file contains JAR files ... and typically other kinds of resources that the web-container understands.
The solution works because you packed all you service classes and depending libraries in one jar. That jar and everything inside will be in the class path and visible to your execution virtual machines class loader.
If you leave your depending libraries out your Jersey Web server needs to have them on it's class path, then you wouldn't get ClassNotFoundExcpetion
You shouldn't pack web application in single jar. You should crate war file where you dependencies will be placed inside WEB-INF/lib. You would easily then deploy that war on any application server. Switching to Maven instead of Ant can help a lot.
EDIT: After you added more details to description and ant
If you don't want to use fat-jar you can either
modify your antjava task to specify classpath that will reference
all external libraries (basically telling ant how to build
-classpath parameter for java -jar command
even better, modify your javac ant task by making complete Manifest file that specifies Class-Path correctly, take a better
look at the solution (at the bottom) of the answer you linked (java build ant file with external jar files)
For completness reference on Manifest here
My single Java file compiles into 10 class files. For Example, Example.java compiles into example1.class, example2.class, etc. How to write an ant build to clean these multiple class files generated for a single java file?
<target name="clean">
<delete>
<fileset dir="${dist}" includes="Example*/*.class"/>
</delete>
</target>
I have a Java project that has just about every class and package depending on everything else. I am trying to untangle it. My current idea is to extract core classes, that shouldn't depend on anything else and package them as a separate JAR that the rest of the application will use. I am using Ant and Netbeans. I am a complete Ant newbie, thought I started reading a book.
I have tried to make an Ant build file that will compile and package the core classes in a JAR. However, no matter what I do, the JAR ends up containing every application class. I am not sure if this is because some of my core classes depend on other classes, that get automatically pulled into the JAR, or because I can't get the Ant directives right. Here is my current build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Core" default="jar">
<description>Compiles and packages Core packages</description>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist"/>
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar" depends="compile">
<jar destfile="dist/core.jar" basedir="build/classes">
<zipfileset dir="build/classes" includes="core/**,xml/**,file/**,exceptions/**"/>
</jar>
</target>
</project>
Any advice?
What is the use of zipfileset entry? Following should be enough to filter in select folders to the jar.
<target name="jar" depends="compile">
<jar destfile="dist/core.jar" basedir="build/classes" includes="core/**,xml/**,file/**,exceptions/**" />
</target>
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.