Domanda

I was following Ant tutorial and wrote a simple program for log4j.But when I am trying to run the JAR by command line I am unable to do so.

Now I am unable to even guess what is the problem.

  • No error is coming up when running JAR via the command line but log file is not updating
  • Log file is updated when app ran in JDeveloper
  • Log file is updated when JAR is ran via Ant script
  • I have already added log4j.properties inside the JAR
  • I have added classpath in commandline


About the JAR
(LoggingTestApp.jar)
META-INF -> MANIFEST.mf
pack ->Mainn.class
log4j.properties

MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 19.1-b02 (Sun Microsystems Inc.)
Main-Class: pack.Mainn

The pack.Mainn class

package pack;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Mainn {
    public Mainn() {
        super();
    }
    String className = this.getClass().getName();

    void logTest() {
        Logger log = Logger.getLogger(className);
        System.out.println("Logging started");
        log.debug(this.className + "Hello this is an debug message");
        log.info(this.className + "Hello this is an info message");
    }

    public static void main(String[] args) {
        System.out.println("Main started");
        Mainn a = new Mainn();
        a.logTest();
    }
}

Ant build script

<?xml version="1.0" encoding="UTF-8" ?>
<project name="LoggingTestApp" basedir="." default="main">
    <property name="src.dir"     value="src"/>
    <property name="lib.dir"     value="lib"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="pack.Mainn"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

Initially some errors of Mainn class not found were coming but I was able to take care of that by looking up.

Now there is one thing that I have been unable to do. I tried the following command to run it but there was some problem due to classpath.

Running directly

java -jar ./LoggingTestApp.jar

Main started
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at pack.Mainn.logTest(Unknown Source)
        at pack.Mainn.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

I looked up and found that Running with jar as part of classpath solved the problem for everybody. When I ran the following command no error came up but the log did not update.

java -cp I:\Study\Codes\_JDeveloper\LoggingTestApp\Client\lib\log4j1.2.13.jar;.\LoggingTestApp.jar pack.Mainn

No error but just

Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32          use a 32-bit data model if available
    -d64          use a 64-bit data model if available
    -server       to select the "server" VM
    -hotspot      is a synonym for the "server" VM  [deprecated]
                  The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.

Now I don't know what to look for. There's no error but using command line to run the JAR is not working at all.

È stato utile?

Soluzione

Try packing the log4j library along with your JAR file

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
            <attribute name="Class-Path" value="."/>
        </manifest>
        <zipfileset excludes="META-INF/*.SF" src="I:/Study/Codes/_JDeveloper/LoggingTestApp/Client/lib/log4j1.2.13.jar"/>
    </jar>
</target>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top