JavaCompiler can output in a different folder?

  • Thread starter =?iso-8859-1?q?Jean-S=E9bastien_Goupil?=
  • Start date
?

=?iso-8859-1?q?Jean-S=E9bastien_Goupil?=

Hi,

I'm currently using Java6 and I'm compiling my class with the
JavaCompiler.

When I compile my class with a specific file, the output .class is
copied just aside the java file.
When I compile my class without specifiying a folder (with the
following code)

///////
final GaeaJavaSourceFromString javaFileObject = new
GaeaJavaSourceFromString("Data", code)
final Iterable<? extends JavaFileObject> compilationUnits =
Arrays.asList(new SimpleJavaFileObject[] { javaFileObject });
final Iterable<String> options = Arrays.asList(new String[] {"-
verbose", "-Xlint" });
boolean compilation = _compiler.getTask(null, _fileManager,
_diagnostic, options, null, compilationUnits).call();

code = "public class Data { }";
_compiler = ToolProvider.getSystemJavaCompiler();
_fileManager = _compiler.getStandardFileManager(null, null, null);
_diagnostic = new DiagnosticCollector<JavaFileObject>();

and
public class GaeaJavaSourceFromString extends SimpleJavaFileObject {
final String code;

public GaeaJavaSourceFromString(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') +
Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
/////////


The output .class is copied in the folder where the program is
started.

I would like to output this class in a specific folder... is it
possible? I know that Eclipse has the property "Output Folder". I
don't know if I should simply move the file ? Is there any option i
can set in the compiler to output the .class file somewhere?
 
R

Richard Reynolds

Jean-Sébastien Goupil said:
Hi,

I'm currently using Java6 and I'm compiling my class with the
JavaCompiler.

When I compile my class with a specific file, the output .class is
copied just aside the java file.
When I compile my class without specifiying a folder (with the
following code)

///////
final GaeaJavaSourceFromString javaFileObject = new
GaeaJavaSourceFromString("Data", code)
final Iterable<? extends JavaFileObject> compilationUnits =
Arrays.asList(new SimpleJavaFileObject[] { javaFileObject });
final Iterable<String> options = Arrays.asList(new String[] {"-
verbose", "-Xlint" });
boolean compilation = _compiler.getTask(null, _fileManager,
_diagnostic, options, null, compilationUnits).call();

code = "public class Data { }";
_compiler = ToolProvider.getSystemJavaCompiler();
_fileManager = _compiler.getStandardFileManager(null, null, null);
_diagnostic = new DiagnosticCollector<JavaFileObject>();

and
public class GaeaJavaSourceFromString extends SimpleJavaFileObject {
final String code;

public GaeaJavaSourceFromString(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') +
Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
/////////


The output .class is copied in the folder where the program is
started.

I would like to output this class in a specific folder... is it
possible? I know that Eclipse has the property "Output Folder". I
don't know if I should simply move the file ? Is there any option i
can set in the compiler to output the .class file somewhere?
yes, did you consider googling this? the very first hit for "javac options"
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javac.html
 
L

Lew

which you really, really should lay out for readability on Usenet.

According to
<http://java.sun.com/javase/6/docs/a...able, java.lang.Iterable, java.lang.Iterable)>

it looks like your "options" are the key.

Richard said:
yes, did you consider googling this? the very first hit for "javac options"
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javac.html

He might've done, except that he's using Java 6, not the obsolescent Java 1.4.

That said, given the information in the JavaCompiler Javadocs, it looks like
the standard compiler options at
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html>
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#options>
apply, specifically "-d directory".
 
?

=?iso-8859-1?q?Jean-S=E9bastien_Goupil?=

which you really, really should lay out for readability on Usenet.


According to
<http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html#g...)>

it looks like your "options" are the key.



He might've done, except that he's using Java 6, not the obsolescent Java 1.4.

That said, given the information in the JavaCompiler Javadocs, it looks like
the standard compiler options at
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html>
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#...>
apply, specifically "-d directory".

I tried that and it seems it's not working...,
I tried with
-d C:\
-d file://C:/
-d .
"-d ."
-d "."
-dC:\

....


And I receive this : "java.lang.IllegalArgumentException: invalid
flag: -d ."
 
R

Richard Reynolds

Jean-Sébastien Goupil said:
I tried that and it seems it's not working...,
I tried with
-d C:\
-d file://C:/
-d .
"-d ."
-d "."
-dC:\

...


And I receive this : "java.lang.IllegalArgumentException: invalid
flag: -d ."
Please show us the whole command you're using.
 
L

Lew

That's probably because "-d" and the target directory are two different
arguments, not one.

How about:

final Iterable<String> options =
Arrays.asList( new String[] { "-verbose", "-Xlint", "-d", "."} );

Then, of course, there is the very dicey issue of where "." is.
 
?

=?iso-8859-1?q?Jean-S=E9bastien_Goupil?=

That's probably because "-d" and the target directory are two different
arguments, not one.

How about:

final Iterable<String> options =
Arrays.asList( new String[] { "-verbose", "-Xlint", "-d", "."} );

Then, of course, there is the very dicey issue of where "." is.

This is perfectly working
of course i don't use ".", it was just to test here

thank you very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top