0

Currently i do some runitime compilation from a src file to a jar with following steps:

First: generate source files in a dir and subdirs

/main    
  /submain1
  /submain2 

Second: Compile code (currently all in .class files go into this directory)

 /builddir

Third: Generate jar from .class file into target folder

 /target

compilation is done by Java ToolProviders Javacompiler

ArrayList<File> files1 =  getSourceFiles();
Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files1);
JavaCompiler.CompilationTask task = compiler.getTask(null ,fileManager , null, optionList, null, compilationUnits1 );
boolean compiled = task.call();

Now my first question is, would it be better to separate all .class files into different compilation units and rebuild the same directory structure as the source files have?

And if so, how can it be done?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Gobliins
  • 3,848
  • 16
  • 67
  • 122
  • I'm surprised you got it working (if you got it working) *without* having the same structure. – Kayaman Oct 21 '15 at 07:29
  • you frighten me... the jar however was created succesfully. But it is not tested tough, since some required components are not implemented yet. – Gobliins Oct 21 '15 at 07:51

2 Answers2

1

Java expects that foo.bar.MyClass is found at foo/bar/MyClass.class (with some additional classpath related requirements), otherwise you will get ClassNotFoundExceptions (or is it NoClassDefFoundErrors) up the wazoo.

So not only should you have the same structure, you must have the same structure. I don't know why you're compiling like that instead of using an existing build tool which would generate correct results by default.

Creating a jar file is equivalent to creating a zip file, so just as a successful compilation doesn't mean that your program works, neither does having a jar file mean it would be a working one.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • It is necessary to compile java code during runtime. So our choice was to use the JavaCompiler provided by jdk programatically. If you have other (better) suggestions, i am open minded for any advice. – Gobliins Oct 21 '15 at 08:00
  • Ah, I didn't realize you were doing that at runtime. – Kayaman Oct 21 '15 at 08:07
  • it would also be possible to execute a commandline or external tool out of java context for compiling but i prefered to stay in the java context – Gobliins Oct 21 '15 at 11:43
  • Technically, you could do anything with the classfiles if you use a custom classloader. – Antimony Oct 21 '15 at 12:56
  • @Antimony What do you mean by *anything* besides...anything? – Kayaman Oct 21 '15 at 13:14
1

Now my first question is, would it be better to separate all .class files into different compilation units and rebuild the same directory structure as the source files have?

The output structure of the .class files must mirror the package names used in your source files. AFAIK folder structure of the source files does not matter that much.

If you use any other structure in your .jar file, classes cannot be found by the class loader.

So the answer to your question is no.

Hendrik
  • 5,085
  • 24
  • 56