File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOW TO COMPILE JAVA FILES PROGAMMATICALLY

 
Durand Grimmett
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this recent
thread

Cheers,
 
Durand Grimmett
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Durand Grimmett
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sergio mendez-rueda
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic