This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

HOW TO COMPILE JAVA FILES PROGAMMATICALLY

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this recent
thread

Cheers,
 
Durand Grimmett
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic