无法通过命令行或双击运行JAR

时间:2014-03-09 15:08:50

标签: java ant command-line jar jdeveloper

我正在关注Ant tutorial并为log4j.But编写了一个简单的程序,当我尝试通过命令行运行JAR时,我无法这样做。

现在我甚至猜不出是什么问题。

  • 通过命令行运行JAR但日志文件未更新
  • 时不会出现错误
  • 当应用程序在JDeveloper中运行时,日志文件会更新
  • 通过Ant脚本
  • 运行JAR时,日志文件会更新
  • 我已经在JAR
  • 中添加了log4j.properties
  • 我在命令行中添加了classpath


关于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

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构建脚本

<?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>

最初未找到Mainn类的一些错误,但我可以通过查找来处理它。

现在有一件事我无法做到。我尝试了以下命令来运行它,但由于类路径存在一些问题。

直接投放

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

我抬起头,发现用jar作为classpath的一部分运行解决了每个人的问题。当我运行以下命令时,没有出现错误,但日志没有更新

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

没有错误,只是

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.

现在我不知道该找什么。没有错误,但使用命令行来运行JAR根本不起作用。

1 个答案:

答案 0 :(得分:1)

尝试将log4j库与JAR文件一起打包

<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>