无法通过命令行或双击运行 JAR [英] Unable to run JAR through command line or double click

查看:42
本文介绍了无法通过命令行或双击运行 JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

  • 通过命令行运行 JAR 时没有出现错误,但日志文件没有更新
  • 在 JDeveloper 中运行应用程序时更新日志文件
  • 通过 Ant 脚本运行 JAR 时更新日志文件
  • 我已经在 J​​AR 中添加了 log4j.properties
  • 我在命令行中添加了类路径


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


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

pack.Mainn 类

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 类的一些错误即将到来,但我能够通过查找来解决这个问题.

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.

直接运行

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

我抬头发现,Running with jar as part of classpath 为大家解决了这个问题.当我运行以下命令时没有出现错误但日志没有更新.

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

没有错误,只是

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 根本不起作用.

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.

推荐答案

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

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>

这篇关于无法通过命令行或双击运行 JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆