无法通过命令行或双击运行JAR [英] Unable to run JAR through command line or double click
问题描述
我在以下蚂蚁教程,并写了一个简单的程序log4j.But当我试图通过运行命令行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时,但日志文件中没有错误快到了没有更新
- 请更新文件
- 当JAR通过Ant脚本运行日志文件更新
- 我已经在JAR中添加log4j.properties
- 我在命令行中添加类路径
当应用程序在JDeveloper跑
:关于这个JAR (LoggingTestApp.jar)
结果META-INF - > MANIFEST.MF
结果包 - > 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
我抬头一看,发现运行与水瓶中的类路径的一部分,解决了每个人的问题。当我跑下面的命令没有错误想出但日志没有更新
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.
推荐答案
尝试用你的JAR文件一起打包的的log4j
库
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屋!