R K
R K

Reputation: 1313

Compile a package using JavaCompiler

Using a java program, I need to compile a package which has a group of .java files, post compilation i need to move .class files to a separate folder.

Instead of using ProcessBuilder class to execute the Java compilation from command line, I decided to use JavaCompiler. I am new to JavaCompiler and I succeeded in my first attempt of compiling one .java file at a time. But I don't know how to set classpath (-classpath), destination folder (-d) and the list of .java files for package level compilation.

Can anyone brief me how to set the above said options?

Upvotes: 0

Views: 883

Answers (1)

Danilo Tommasina
Danilo Tommasina

Reputation: 1760

As suggested by Harry Joy, you may use ANT. What he probably meant is not using it from the command-line respectively with a build.xml file, but using it directly from your java program. This way you will have access to the 'fileset/exclude/include...' functionality that you have in ant and save a lot of coding.

Example, pseudo code (not tested and probably not even compiling, just as hint):

Project p = new Project();
p.init();
p.addBuildListener( new SimpleBuildListener() );
p.setBaseDir( new File( "." ).getAbsoluteFile() );

Javac task = (Javac) p.createTask( "javac" );
task.srcdir( srcDirPath );

// Filsets can be built this way
FileSet set = new FileSet();
set.setDir( srcDirPath );
set.setIncludes( "**/*.java" );

task.addFileset( set );

Upvotes: 1

Related Questions