2

I try to use JavaCompiler to compile source code.

class A{int i;}; 
class B extends A{i = 5;};

The problem is even if they are in the same folder, When compiling class B, JavaCompiler still can't find Class A.

So, I am wondering the problem is I didn't add the path of the folder to classPath.

I don't know how to do it in java code, So didn't give it a shot.

Thanks for any help.

MByD
  • 135,866
  • 28
  • 264
  • 277
RobinBattle
  • 220
  • 4
  • 14
  • 1
    Hm, ain't sure, but probably that's because your code has an error. You don't write things like `class B extends A{i = 5;}`, use an init block (or better, a constructor) to set a member value: `class B extends A{ {i = 5;} };` – rlegendi Feb 07 '12 at 10:15
  • *"I don't know how to do it in java code, So didn't give it a shot."* That is not an excuse to not to read the fine manual. -1 – Andrew Thompson Feb 07 '12 at 10:21
  • I type it wrong... aioobe gives a correct solution. I should add -cp argument to java compiler. – RobinBattle Feb 07 '12 at 10:22

3 Answers3

4

You need to set the class path for the compile task.

Have a look at the answer over here:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

another point of view would be to generate directly the bytecode using one of the famous tools for such task like ASM, JavaAssist, SERP or any other one..... It could be a very good way to avoid : - path problems - to have a finer control on the process (if you have javac errors you will be obliged to parse the stream to raise thme into your application) - improve the whole process performance

But it adds some complexity... Like often it's a trade off

aioobe
  • 413,195
  • 112
  • 811
  • 826
romje
  • 650
  • 3
  • 4
0

extend the classpath to the current directoy.

You can do that via the -classpath option or the CLASSPATH variable.

-claspath=. 

or

CLASSPATH=.
Jörg Beyer
  • 3,631
  • 21
  • 35