使用 Ant 执行 JAR 文件时出错"Cannot load com.apple.laf.AquaLookAndFeel"



我有这个 Ant 文件:

<?xml version="1.0"?>
<project name="tetris" default="all" basedir=".">
    <description>
        simple example build file
</description>
<!-- set global properties for this build -->
<property name="name" value="tetris"/>
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="dist" location="dist"/>
<property name="classpath" value="${dist}/${name}.jar"/>
<property name="main" value="com.els.test.Game"/>
<target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the bin directory structure used by compile -->
    <mkdir dir="${bin}"/>
</target>
<target name="compile" depends="init"
                        description="compile the source">
    <!-- Compile the java code from ${src} into ${bin} -->
    <javac srcdir="${src}" destdir="${bin}" debug="on" deprecation="on"/>
</target>
<target name="dist" depends="compile"
                        description="generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>
    <!-- Put everything in ${bin} into the MyProject-${DSTAMP}.jar file -->
    <!-- <jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${bin}"/> -->
    <jar jarfile="${dist}/${name}.jar" basedir="${bin}"/>
</target>
<target name="run" depends="dist"
                        description="Run the program">
    <java classname="${main}"
                        classpath="${classpath}"/>
</target>
<!-- Delete the ${bin} directory trees -->
<target name="clean"
                description="clean up">
    <delete dir="${bin}"/>
    <mkdir dir="${bin}"/>
    <delete dir="${dist}"/>
    <mkdir dir="${dist}"/>
</target>
<target name="all" depends="clean,compile,dist,run"/>
</project>

它报告如下错误:

      [jar] Building jar: /Users/richardlee/Code/tetris/tetrisCode/dist/tetris.jar
run:
     [java] java.lang.Error: Cannot load com.apple.laf.AquaLookAndFeel
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:198)
     [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:834)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:228)
     [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:137)
     [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:110)
     [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
     [java]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
     [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
     [java]     at org.apache.tools.ant.Target.execute(Target.java:435)
     [java]     at org.apache.tools.ant.Target.performTasks(Target.java:456)
     [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
     [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
     [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
     [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
     [java]     at org.apache.tools.ant.Main.runBuild(Main.java:857)
     [java]     at org.apache.tools.ant.Main.startAnt(Main.java:236)
     [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287)
     [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113)
     [java] Caused by: java.lang.Error: Cannot load com.apple.laf.AquaLookAndFeel
     [java]     at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1351)
     [java]     at javax.swing.UIManager.initialize(UIManager.java:1459)
     [java]     at javax.swing.UIManager.maybeInitialize(UIManager.java:1426)
     [java]     at javax.swing.UIManager.getUI(UIManager.java:1006)
     [java]     at javax.swing.JPanel.updateUI(JPanel.java:126)
     [java]     at javax.swing.JPanel.<init>(JPanel.java:86)
     [java]     at javax.swing.JPanel.<init>(JPanel.java:109)
     [java]     at javax.swing.JPanel.<init>(JPanel.java:117)
     [java]     at com.els.view.GamePanel.<init>(GamePanel.java:21)
     [java]     at com.els.test.Game.main(Game.java:18)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:221)
     [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:154)
     [java]     ... 20 more
     [java] Java Result: -1
all:
BUILD SUCCESSFUL
Total time: 1 second

但是我可以使用命令"java ..."运行.class文件:

cd bin
java com.els.test.Game

构建.xml文件是怎么回事?

我在macOS Catalina 10.15.1上运行 jdk.java.net 的Java JDK-14和 ant.apache.org 的Ant apache-ant-ant-1.10.7。话虽如此,在build中为java任务添加fork="true">.xml解决了这个问题。构建.xml文件附在下面:

<project name="Threads" default="dist" basedir=".">
    <description>
        Decision screen.
    </description>
    <property name="src" location="sources"/>
    <property name="build" location="classes"/>
    <property name="dist" location="dist"/>
    <property name="resources" location="resources"/>
    <target name="init">
        <tstamp/>
        <mkdir dir="${build}"/>
        <mkdir dir="${dist}"/>
    </target>
    <target name="compile" depends="init" description="compile the source">
        <!-- Compile the Java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>
    </target>
    <target name="DecisionScreen" depends="compile" description="Run program DecisionScreen">
        <!-- HERE IS THE CHANGE -->
        <java classname="Decision.DecisionScreen" fork="true">
            <classpath>
                <pathelement path="${build}"/>
            </classpath>
        </java>
    </target>
    <target name="dist" depends="compile" description="generate the distribution">
        <!-- Create the distribution directory -->
            <mkdir dir="${dist}/lib"/>
            <jar jarfile="${dist}/lib/Decision-${DSTAMP}.jar" basedir="${build}">
                <manifest>
                    <attribute name="Main-Class" value="Decision.DecisionScreen"/>
                </manifest>
            </jar>
    </target>
    <target name="clean" description="clean up">
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>
</project>

按照上面的构建中所述设置目录结构.xml编译后的类可以使用以下任一方法运行:

$ ant DecisionScreen
$ java -cp classes Decision.DecisionScreen
$ java -jar dist/lib/Decision-20200306.jar

"java.lang.Error: 无法加载 apple.laf.AquaLookAndFeel"不再出现。

相关内容

  • 没有找到相关文章

最新更新