Javac compile unreferenced files

30 views Asked by At

I noticed a severe difference in behaviour between the compiled application and when i run it from within my IDE (Eclipse). Digging in log files, results that in the compiled application some ClassNotFoundException are thrown when specific classes are loaded. These classes are not referenced from within the code, but only loaded at runtime using reflection (the class names stored in a config file). Within the IDE this works perfectly, in the compiled version the class files are missing.

My compile instructions look like this (shortened for better understanding):

javac -d ../compiled com/mycompany/projectname/init/MainClass.java

According to a brief research on SO, this is not wat I expected. But at a closer look it is right: MainClass is compiled and all dependencies as well. The files that are not referenced, are not compiled.

  1. The first workaround I worked out, is to import the files somewhere in another source file, so that they are referenced. I am aware that this is not a proper solution, since in future I might add another file and forget about the "useless" import, or worse i might delete the import statements to get rid of the warnings.

  2. Second workaround is to compile at two stages, first the folder where the unreferenced files lie, then the whole project.

    javac -d ../compiled com/mycompany/projectname/comparators/*

    followed by

    javac -d ../compiled com/mycompany/projectname/init/MainClass.java

    The first compile command will compile all files within the comparators directory, regardless of them being referenced anywhere, the second command will compile the MainClass and all its references. Since both compilation processes export to the same directory, I will then have all needed files in the compiled folder.

Is there any way to do this with only one compilation-run and without the need to list all files explicitly?

I tried following command

javac -d ../compiled com/mycompany/projectname/* but got an error invalid flag: com/mycompany/projectname This is probably caused by the fact, that there are no files, only folders under com/mycompany/projectname/

0

There are 0 answers