log4j.properties configuration issues along with ant configuration - java

I've spent a lot of time reading the posts on stackoverflow regarding log4j and the different ways it can be implemented. I've decided to take the approach of log4j.properties. I am still running into issues when I run from within eclipse or from ant with:
log4j:WARN No appenders could be found for logger (My.Class).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I've done two things thus far:
Created the log4j.properties file in the root of the /src folder
Moved the log4j.properties file into the package where the .java file exists.
Both instances produce the same issue. Here is the code I am using in my class:
private static Logger log = Logger.getLogger(My.class);
I have read over and over that the properties file needs to be in the classpath. I feel like i have done that, but maybe not. Here is my package structure:
src
packageA
myclass.java
log4j.properties (attempt 2)
packageB
packageC
packageD
log4j.properties (attempt 1)
Let me point out I'd like every java file in all the packages to use the same log4j.properties. If there is an easier way to configure this other than redundantly coping props files around let me know. I was thinking I'd have to switch to using a resource loader.
Moving on to my ant issue:
I have the exact same issue in ant. I have added what I thought I needed from what I read on apache's site to get ant to run w/o issue, but to no avail.
here is the additional entry in my compile target...
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar">
<include name="**/*.properties"/>
</fileset>
</path>
<target>
<javac srcdir="${source.dir}" destdir="${classes.dir}" classpathref="classpath" debug="true" debuglevel="lines,vars,source" includeAntRuntime="false"/>
<copy todir="${classes.dir}">
<fileset dir="${source.dir}" excludes="**/*.java"/>
</copy>
</target>
I should also note my junit target has the included classpath.
I also attempted adding the -Dlog4j.configuration=file:/path to file and I still see the warnings after that... I'm at a loss.
I was under the impression this would copy my .properties file into the classpath of ant.

for ant i added:
<path id="properties">
<dirset dir="${config.dir}"/>
</path>
where ${config.dir} is the path to the /src/config folder where the log4j.properties exists.
Still working on getting eclipse to work, it shouldnt be too different with classpaths in eclipse.
Thanks!

What I've found is that my log4j.properties must be in the root of the classpath.
Since my classpath src includes test code in the applybc2014 package, I've put my log4j.properties there. Note, I'm also excluding ONLY java files.
<property name="src.dir" value="src/ca/bccampus/tests/applybc2014"/>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" classpathref="classpath"/>
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>

Related

Specification of configuration file (log4j2.xml) for log4j as VM argument just working in eclipse

I'm using the new Log4j2 - Java Logging Framework. If I specify the path to the configuration file in eclipse as VM argument -Dlog4j.configurationFile=/home/../config.xml everything works fine. The configure file is loaded and the logging works as expected, i.e. all logs are written to files. If I additionally use the -Dlog4j.debug, I get the corresponding debug messages from the framework which confirms the correct loading of my configuration file.
However, when I use ANT with a build.xml file the logging framework seems to be initialized with the default configuration - the logs no longer written to the files but to the console. I start the created prog.jar file with the following statement from the console:
java -Dlog4j.configurationFile=/home/../config.xml -Dlog4j.debug -jar prog.jar
The only debug message I get from to Log4j2 framework is the following:
DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!
In the following, parts of the build.xml file which I use to create prog.jar:
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<path id="master-classpath">
<fileset dir="${lib}/apache-log4j-2.11.1-bin/">
<include name="log4j-api-2.11.1.jar"/>
<include name="log4j-core-2.11.1.jar"/>
</fileset>
</path>
<target name="compile" depends="init" description="compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" includeantruntime="true">
<classpath refid="master-classpath"/>
</javac>
</target>
<target name="jar" depends="compile" description="generate the distribution">
<mkdir dir="${dist}"/>
<jar jarfile="${dist}/prog.jar" basedir="${build}" compress="true">
<fileset dir="${src}" includes="**/*.java"/>
<zipgroupfileset dir="${lib}/apache-log4j-2.11.1-bin/" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="ch.zwas.aks.Runner"/>
</manifest>
</jar>
</target>
I'm confused why it works using Eclipse but does apparently not work when I create the project with ANT and specify the configuration file in the console. Furthermore, I have no idea why there are no more debug messages when I run the jar from the console.
Thanks for your support.
Thanks to the hint of Vikas Sachdeva I was able to solve the problem. The problem was that I apparently packed too many jar files from the log4j library into prog.jar. When I include just the following jar files it works as expected with ANT / console as well.
junit-X.jar
log4j-api-X.ja
log4j-core-X.jar
slf4j-api-X.jar
slf4j-simple-X.jar
The other jars which came with the log4j library I left out. It seems that Eclipse ignores these, but when included with the build.xml into to finally jar then they cause trouble.

Java Ant Job how to ship Log4j jar with my jar

Just trying to upgrade some old stuff and part of that I need to bundle my custom jar which uses Log4j. I did add the following for my <javac> task which compiles successfully.
<path id="my.classpath">
<fileset dir="${mainpath}">
<include name="**/*log4j*.jar"/>
</fileset>
</path>
<javac srcdir="src/java" destdir="build/filez/java" debug="on" deprecation="no"
includes="my/instruments/**/*, org/apache/log4j/**/*">
<classpath refid="my.classpath"/>
</javac>
However, In my <jar> job I cannot see any log4j dependency packed with my custom jar. This might be a silly question, but how do ensure that my custom-jar does not fail when called from another application since the dependency isn't packed? Will it be okay as long as log4j has been loaded by classloader in the target application?
Additionally, do I need to add something in my Manifest for this?
I cannot use Maven (yes I know) for a little while, so cannot solve this problem with maven
You can use One-JAR to package your code along with it's dependencies into one big executable JAR.
It can be used either as a standalone tool from the command line or as a task defined in build.xml.
<!-- Construct the One-JAR file -->
<one-jar destfile="hello.jar" manifest="hello.mf">
<main>
<!-- Construct main.jar from classes and source code -->
<fileset dir="${classes.dir}/src"/>
</main>
<lib>
<fileset file="${build.dir}/lib.jar" />
</lib>
</one-jar>

Ant can't find classpath classes

So I'm extending my company's ant build script to add in a special module we want build in some cases. I've written an ant script that points to where I know the compiled class files for the rest of our codebase are, because they get compiled earlier in the build process. I know with 100% certainty the files are in this location.
However, whenever I try to compile this module, the classpath reference can't see those classes, and I get a bunch of "package does not exist" and "can't find symbol" errors.
I just can't seem to figure out what I'm doing wrong. Hoping for help here.
Here's my build script code:
<property name="classpath" value="${dir.dev}/out/production/Main"
<path id="pfClasspath">
<fileset dir="${classpath}">
<include name="**/*.class"/>
</fileset>
<fileset dir="${dir.dev.lib}">
<include name="**/*.jar" />
</fileset>
<fileset file="${lib.json}" /> <!-- TODO try removing this -->
</path>
<target name="compile" depends="prepare">
<javac source="1.7" classpathref="pfClasspath" srcdir="${dir.project}/src" destdir="${dir.project.build.classes}" />
</target>
The directory the "classpath" property is pointing at 100% contains all of the class files for the rest of the project. That level is the equivalent of the "src" directory on the sources side, immediately within it are the com/companyName/etc... folders.
My code contains references to the classes compiled at this location. Yet ant isn't finding them. Any help?
Try
<path id="pfClasspath">
<pathelement path="${classpath}" />
...
</path>
instead. Specifying the classpath does not mean to specify every single class file that's on the classpath, which is what you do when you define your <path> element using a <fileset>.

Why combine all jars together?

I have create RESTful web service based on the JAX-RS and used Jersey embedded web server. My ant script compiles code successfully while it gives me error ClassNotFoundException when I run my main class. So after doing research I came up with solution & here it goes java build ant file with external jar files . What I did was created a bundled jar file try to execute that & it works perfectly fine. I want to know the reason behind :
why this solution works ?
Why I should combine all jar file ?
Is it similar to war file which we create following J2EE architecture otherwise war will not be extracted by server ( say TOMCAT ) & in my case jar file for Jersey embedded HTTP server?
EDIT:
Here is my ant build.xml file
<property name="lib.dir" value="${user.dir}/lib"/>
<property name="build.dir" value="${user.dir}/build"/>
<property name="build.lib.dir" value="${build.dir}/lib"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="src.dir" value="${user.dir}/src/main/java"/>
<property name="main.class" value="com.assignment.ConsoleServer"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="copy_jars" depends="init" >
<copy todir="${build.lib.dir}" >
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy_jars">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="classpath" includeantruntime="false"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.dir}/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
Here is my folder structure
P.S. I am not java expert so pardon me if this question is stupid.
Why this solution works?
In your particular case, you probably didn't include all of the necessary dependencies in your deployment in your previous. (It is not clear from your question how you were originally doing the deployment.)
Now you have put all of the application and dependent class files, etc into one JAR file, and presumably you are deploying / running that file. It works because now it has everything that it needs to run ... which it didn't before.
Why I should combine all jar file?
In your case I suspect that it was not strictly necessary. There was probably a way to "deploy" all of the dependencies without combining them into a single JAR file.
However, there is one case where a "uber-jar" has advantages. That is when the JAR is intended to be an "executable" JAR, and you want to be able to distribute / install it as a single file. (And executable JAR
file can refer to external JARs, etc, but the way that you have to do
it is "fragile".)
Is it similar to war file ... ?
Sort of, though a WAR file contains JAR files ... and typically other kinds of resources that the web-container understands.
The solution works because you packed all you service classes and depending libraries in one jar. That jar and everything inside will be in the class path and visible to your execution virtual machines class loader.
If you leave your depending libraries out your Jersey Web server needs to have them on it's class path, then you wouldn't get ClassNotFoundExcpetion
You shouldn't pack web application in single jar. You should crate war file where you dependencies will be placed inside WEB-INF/lib. You would easily then deploy that war on any application server. Switching to Maven instead of Ant can help a lot.
EDIT: After you added more details to description and ant
If you don't want to use fat-jar you can either
modify your antjava task to specify classpath that will reference
all external libraries (basically telling ant how to build
-classpath parameter for java -jar command
even better, modify your javac ant task by making complete Manifest file that specifies Class-Path correctly, take a better
look at the solution (at the bottom) of the answer you linked (java build ant file with external jar files)
For completness reference on Manifest here

Log4j not being added to classpath in Ant

I'm attempting to add Log4j to my project's classpath in Ant which creates an executable JAR, but it appears that it's not being added properly.
Here is the path component of my Ant build script:
<path id="classpath.compile">
<fileset dir="${dir.myLibs}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>
The compile target looks like this:
<target name="-compile">
<javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
<src path="${dir.source}"/>
<classpath refid="classpath.compile"/>
</javac>
</target>
Tthe target that creates the JAR:
<target name="-createJar" >
<jar jarfile="${path.jarFile}"
manifest="${dir.source}\META-INF\MANIFEST.MF">
<fileset dir="${dir.binaries}" casesensitive="yes">
<exclude name="**/*.java"/>
</fileset>
</jar>
</target>
Lastly, the MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar
The JAR is created, but when I execute it, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...
Any thoughts as to what I'm doing wrong?
It looks from the classpath in your MANIFEST that you are trying to reference a jar inside your jar. The only two ways to make that work AFAIK are 1) a special classloader, like #infosec812 mentions, or 2) by exploding the jar dependencies directly into the root of your jar. Either is workable, but I don't see either of them happening in your ant script.
If you're trying to reference a jar outside of your jar, your relative classpath is relative to the location of the jar you are executing. Make sure the referenced jar exists in that location.
I'm guessing that you're running the Java program as follows
java -jar myapp.jar
In this case you'll need to specify the Class-Path attribute in the manifest. I suggest you also check out the manifestclasspath task
Creating the jar does not include the linked libraries in the jar. You would have to have the required jars in your execution classpath in order to run it that way. Or, you could use the solution I use, which is to create a one-jar archive. It adds a specialized class loader for your application into the resulting jar and also packages your required jars in to the final executable jar. It works really well for deploying neat, simple to use packages.

Categories

Resources