File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HOW TO COMPILE JAVA FILES PROGAMMATICALLY Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HOW TO COMPILE JAVA FILES PROGAMMATICALLY" Watch "HOW TO COMPILE JAVA FILES PROGAMMATICALLY" New topic
Author

HOW TO COMPILE JAVA FILES PROGAMMATICALLY

Durand Grimmett
Greenhorn

Joined: Jun 02, 2004
Posts: 16
Does anyone have any sample code that will take some java files and compile them programmatically.....that is without using the command prompt?....Any help will be greatly appreciated.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Have a look at this recent
thread

Cheers,


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Durand Grimmett
Greenhorn

Joined: Jun 02, 2004
Posts: 16
The thread that you pointed me to does contain some useful info....but do you have any other examples or sample code that you can refer me too.
I actually just looking for a simple method that I can use, that takes some .java files and compiles them to .class files

Thanks in advance for your help
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
The simplest way that I know of would be to write a batch file that invokes the compiler. You could then schedule the batch file to run at intervals if you wanted. Ant would handle this too, I believe.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Durand Grimmett
Greenhorn

Joined: Jun 02, 2004
Posts: 16
Question...is it possible to invoke an ant task within a java program? If so how?

next....does anyone have an example of an ant task that compiles java files..........or compiles and jars java files?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Ant tasks are classes. You can invoke them the same way you invoke any other Java class. I think Jeff's way off with the ant suggestion. The thread Tom pointed you has a link to this article which demonstrates how to directly invoke the java compiler. With source code and all.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
sergio mendez-rueda
Ranch Hand

Joined: May 12, 2005
Posts: 37
Hi Durand,

next....does anyone have an example of an ant task that compiles java files..........or compiles and jars java files?

i think, this simple ant file (build.xml) is the one you are looking for:
<?xml version="1.0"?>
<project name="Java development" default="usage" basedir=".">
<!-- simple ant file
Sun Jun 12 17:40:55 2005
@author s. mendez
-->

<!-- =================================================================== -->
<!-- the name of the wep application -->
<!-- =================================================================== -->

<property name="app.name" value="JavaRanchExemple"/>

<!-- =================================================================== -->
<!-- properties reflecting the directory structur and properties of the -->
<!-- web-app development -->
<!-- =================================================================== -->

<property file="${basedir}/build.properties"/>
<property name="java.src" location="${basedir}/src"/>
<property name="java.src.meta" location="${java.src}/META-INF"/>
<property name="java.src.meta.tags" location="${java.src.meta}/tags"/>
<property name="java.classes" location="${basedir}/classes"/>
<property name="jar.name" value="${app.name}.jar"/>
<property name="dist.dir" location="${basedir}/dist"/>

<!-- =================================================================== -->
<!-- claspath of the wep application -->
<!-- =================================================================== -->
<!--
<path id="build.classpath">
<fileset dir="${tomcat.home}/common/lib/">
<include name="servlet-api.jar"/>
<include name="jsp-api.jar"/>
</fileset>
<fileset dir="${tomcat.home}/common/endorsed/">
<include name="*.jar"/>
</fileset>
<fileset dir="${lib}">
<include name="*.jar"/>
</fileset>
</path>
-->

<!-- =================================================================== -->
<!-- Create the file and directory structure of the web app -->
<!-- =================================================================== -->

<target name="init">
<echo>+ ===================================================== +</echo>
<echo>+ Creating file and directory structure of the app +</echo>
<echo>+ ===================================================== +</echo>
<mkdir dir="${java.src}"/>
<mkdir dir="${java.src.meta}"/>
<mkdir dir="${java.src.meta.tags}"/>
<mkdir dir="${java.classes}"/>
<mkdir dir="${dist.dir}"/>
</target>

<!-- =================================================================== -->
<!-- usage of this file -->
<!-- =================================================================== -->

<target name="usage">
<echo message=""/>
<echo message="your are running the app '${app.name}'"/>
<echo message="-------------------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="usage --> this."/>
<echo message="init --> Create the file and directory structure of the app"/>
<echo message="compile --> Compile main source tree java files"/>
<echo message="jarFile --> Build the Jar file"/>
<echo message=""/>
</target>

<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->

<target name="compile" depends="init"
description="Compile main source tree java files">
<echo>+ ===================================================== +</echo>
<echo>+ Compiling java files +</echo>
<echo>+ ===================================================== +</echo>
<javac srcdir="${java.src}" destdir="${java.classes}"> </javac>
</target>

<!-- =================================================================== -->
<!-- Building the Jar file -->
<!-- =================================================================== -->

<target name="jarFile" depends="compile" description="build the jar file">
<echo>+ ===================================================== +</echo>
<echo>+ Building jar files +</echo>
<echo>+ ===================================================== +</echo>
<delete>
<fileset dir="${dist.dir}" includes="${jar.name}" />
</delete>

<jar basedir="${java.classes}" excludes="**" jarfile="${dist.dir}/${jar.name}">
<fileset dir="${java.classes}">
<include name="**/*.class" />
<exclude name="**/web/**/*" />
<exclude name="**/test/**/*" />
</fileset>
<metainf dir="${java.src}/META-INF" includes="*.xml"/>
<metainf dir="${java.src}/META-INF" includes="*.tld"
excludes="application.xml" />
<metainf dir="${java.src}/META-INF/tags" includes="*.tag"/>
</jar>
</target>
</project>

enjoy ant
ser
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOW TO COMPILE JAVA FILES PROGAMMATICALLY