Hi Emma,

Yes, I think there is something awry with the log4j-api-2.11.1.jar.  To get
it working just using ModulePath, I first:

1) Opened the log4j-api-2.11.1.jar file in Archive Manager

2) Copied the module-info.class file from /META-INF/versions/9/ folder to
the root of the archive

After both the api and core jars are (re)added to your module path, you can
use "requires org.apache.logging.log4j;" in your own module-info file.
Netbeans will find this in code completion. :)

What I believe happens during classloading is first the release specific
location is checked, and if not it falls back to the default (root)
location.  Loading via ModulePath requires a compiled module-info in the
"base" folder of the classes.
In the case of the log4j-api-2.11.1.jar file, the release specific location
(/META-INF/versions/9/) contains only only one class - presumably the one
and only class with Java9 specific code.

However, when class loading falls back to the root to find the other
classes, there is no module-info there, which is why I believe it fails.

As to whether best practice would be to have the whole
org.apache.logging.log4j under /META-INF/versions/9/, or to have a
module-info in the root as I did, I don't know enough to tell you.



Full steps below to create the project and reference the clases, if you
need them:

1) Netbeans -> File -> New Project -> Java Modular Project  call it
"Log4j2TestCasePDW" and platform JDK10

2) Right click Project node -> New -> Module  and call it "Log4j2TestCase".

3) Right click Project node -> Properties -> Libraries and the click
"Browse" next to the library folder.
       Accept the defaults to create a /lib folder and to copy libraries to
the folder

4) Still in the Libraries properties, using the ellipsis (...) next to
ModulePath, choose "Add JAR/Folder" and add the following two .jar files
(making sure "copy to library folder" is checked:
       log4j-api-2.11.1.jar
       log4j-core-2.11.1.jar

N.B.  For any Netbeans devs reading, there seems to be a bit of a minor bug
in NB here - if you highlight both and try and add them both at the same
time, one JAR will go in ModulePath and one in ClassPath.

5) Navigate to your Classes node -> <default package>, open
module-info.java and add the following line:
   requires org.apache.logging.log4j.core;

N.B. If you press ctrl-space to bring up code completion after typing
"requires", you will see the modules you can import.  the
"org.apache.logging.log4j"

6) Copy your test package and .java file under the Classes node

7) Right click -> Run on your Log4j2TestCase.java file


You will get a console output similar to:

"02:20:00.367 [main] ERROR log4j2testcase.Log4j2TestCase -
 *** Log4j2 error message"


Hope that helps,

P


On Wed, Oct 3, 2018 at 12:51 PM Emma Atkinson <emma.atkins...@gmail.com>
wrote:

> To recreate the test case and Netbeans set up.
>
>    - I created a Java Application using the new project function.  I set
>    it up to run on JDK 10.
>
>
>    - I added a module-info.java file using a RMB on Project Navigator >
>    Log4j2TestCase > Source Packages > New > Java Module Info...
>
>
>    - I created a Library entry (named Apache Log4j2 2.11) that refers to
>    the log4j-api-2.11.1.jar via Tools > Libraries pressing New Library...
>    button.  I only added the one jar, no javadoc, no sources.
>
>
>    - Using a RMB on the Project Navigator > Log4j2TestCase > Libraries >
>    Add Library... I added the Library entry named Apache Log4j2 2.11.
>
>
>    - Using a RMB on the Project Navigator > Log4j2TestCase > Libraries >
>    Add JAR/Folder ... I added a separate reference to log4j-api-2.11.1.jar and
>    log4j-core-2.11.1.jar.  Netbeans put the log4j-core-2.11.1.jar on the
>    modulepath and added a line to my module-info.java.
>
>
>    - RMB on Project Navigator > Log4j2TestCase  selected Properties >
>    Libraries.  I moved the reference to the jar files down into the classpath.
>
>
>    - I edited the module-info.java file to delete the automatically added
>    line
> * requires *org.apache.logging.log4j.core*;*
>
>
>    - I added two import statements to the Log4j2TestCase.java (containing
>    the main entry point)
> * import* org.apache.logging.log4j.LogManager;
> * import* org.apache.logging.log4j.Logger;
>
> As it stands Log4j will not find its configuration XML file so it will say
> that it is using default setting which means it output errors and above and
> suppresses messages that are warnings, info and debug.
>
>    - I created an object called LOGGER.
>
>
>    - I created a LOGGER.error("some text "); so that it would output to
>    the console under default settings if it ran.
>
> But first we need to build the application.
>
>    - Select the module-info.java file and using a RMB menu option to
>    Compile File.  The ant build script will run and eventually output an error
>    message saying that it cannot find the module log4j.api.
>
> I hope this is comprehensive enough.
>
>
>
>

Reply via email to