JavaCompiler example -- trouble with finding class definitions

R

Robert Dodier

Hello,

I am working with two examples of using the JavaCompiler class in JDK
6:
http://groups.google.com/group/comp.lang.java.programmer/msg/0243d70fbce78e52?dmode=source&hl=en
http://groups.google.com/group/comp.lang.java.programmer/msg/3265954fdaac103e?dmode=source&hl=en

I find that, after making just a few minor modifications
(JavaCompilerTool --> JavaCompiler, getResult --> call,
getStandardFileManager(diagnostics) -->
getStandardFileManager(diagnostics, null, null))
the examples run as expected. (The JavaCompiler stuff has changed
since the examples were written.) Thanks very much to the authors.

I can also modify the source code strings so that functions
such as Math.sin or Math.exp are called, and these work as expected.

However, I find that when I try to call some method which I wrote
(e.g. with FOOBAR defined as public class FOOBAR { public FOOBAR () {}
public double FOO (double x) { return x*x; }}, I try FOOBAR f = new
FOOBAR ();
f.FOO(2.0); in the source code string) then the compilation fails with
"cannot find symbol
symbol : class FOOBAR"
which is disappointing.

I tried specifying the classpath in different ways -- e.g. -cp . or
-Xbootclasspath/a:. or -cp FOOBAR.jar (after putting FOOBAR.class into
a jar file) or export CLASSPATH=. but none of those help java find
FOOBAR.

If I cut-n-paste the source code into a file, and compile it on the
command
line, then it works OK.

I wonder if anyone has some words of wisdom here.
Thanks in advance for your help.

Robert Dodier
 
H

hiwa

Robert said:
Hello,

I am working with two examples of using the JavaCompiler class in JDK
6:
http://groups.google.com/group/comp.lang.java.programmer/msg/0243d70fbce78e52?dmode=source&hl=en
http://groups.google.com/group/comp.lang.java.programmer/msg/3265954fdaac103e?dmode=source&hl=en

I find that, after making just a few minor modifications
(JavaCompilerTool --> JavaCompiler, getResult --> call,
getStandardFileManager(diagnostics) -->
getStandardFileManager(diagnostics, null, null))
the examples run as expected. (The JavaCompiler stuff has changed
since the examples were written.) Thanks very much to the authors.

I can also modify the source code strings so that functions
such as Math.sin or Math.exp are called, and these work as expected.

However, I find that when I try to call some method which I wrote
(e.g. with FOOBAR defined as public class FOOBAR { public FOOBAR () {}
public double FOO (double x) { return x*x; }}, I try FOOBAR f = new
FOOBAR ();
f.FOO(2.0); in the source code string) then the compilation fails with
"cannot find symbol
symbol : class FOOBAR"
which is disappointing.

I tried specifying the classpath in different ways -- e.g. -cp . or
-Xbootclasspath/a:. or -cp FOOBAR.jar (after putting FOOBAR.class into
a jar file) or export CLASSPATH=. but none of those help java find
FOOBAR.

If I cut-n-paste the source code into a file, and compile it on the
command
line, then it works OK.

I wonder if anyone has some words of wisdom here.
Thanks in advance for your help.

Robert Dodier
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html
 
R

Robert Dodier

Hello,

I've resolved the problem with the examples which I mentioned before.
It turns out the problem is very simple: The examples generate
code which is declared to be in package just.generated .
So the class FOOBAR needs to also be in package just.generated
(or, more generally, any referenced class needs to be visible
to classes in the package just.generated).

When I add "package just.generated;" to FOOBAR.java and
recompile it (using the command line javac) then the examples
work as expected when the generated code references
just.generated.FOOBAR .

Thanks again to the authors of the examples, which are very helpful.
Hope this info is useful to someone.

Robert Dodier
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top