这个java类路径设置正确吗?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (41)

我真的不知道我错过了什么。我花了很多时间阅读如何设置类路径,但这仍然不起作用,此时我确信它一定是一些微不足道的东西逃脱了我。

我正在尝试使用swing radiance库来改变swing应用程序的外观和感觉。我按照构建文档来制作jar文件,当这些文件无效时,我下载并尝试了预构建的二进制文件。我向开发人员提出了一个问题,并确认我正在按照正确的入门说明进行操作。演示代码如下:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

public class Walkthrough extends JFrame {
  public Walkthrough() {
    super("Sample app");
    this.setLayout(new FlowLayout());
    this.add(new JButton("button"));
    this.add(new JCheckBox("check"));
    this.add(new JLabel("label"));

    this.setIconImage(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR));
    this.setSize(new Dimension(250, 80));
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

    SwingUtilities.invokeLater(() -> {
        Walkthrough w = new Walkthrough();
        w.setVisible(true);
    });
  }
}

代码首先编译javac Walkthrough.java并执行java Walkthrough。这很好用。接下来,使用辐射罐来改变外观,这是通过将swing.defaultlaf属性设置为使用来自辐射的外观来完成的。这是它不起作用的地方。

我将所有必需的jar复制到一个tmp目录中,其中Walkthrough示例紧接在其中tmp。然后我尝试按如下方式运行应用程序:

java -Dswing.defaultlaf=org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel -cp .:tmp/radiance-substance-2.5-SNAPSHOT.jar:tmp/radiance-trident-2.5-SNAPSHOT.jar:tmp/radiance-neon-2.5-SNAPSHOT.jar Walkthrough 

这导致以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot load org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel
    at java.desktop/javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1406)
    at java.desktop/javax.swing.UIManager.initialize(UIManager.java:1517)
    at java.desktop/javax.swing.UIManager.maybeInitialize(UIManager.java:1483)
    at java.desktop/javax.swing.UIManager.getUI(UIManager.java:1056)
    at java.desktop/javax.swing.JPanel.updateUI(JPanel.java:126)
    at java.desktop/javax.swing.JPanel.<init>(JPanel.java:86)
    at java.desktop/javax.swing.JPanel.<init>(JPanel.java:109)
    at java.desktop/javax.swing.JPanel.<init>(JPanel.java:117)
    at java.desktop/javax.swing.JRootPane.createGlassPane(JRootPane.java:521)
    at java.desktop/javax.swing.JRootPane.<init>(JRootPane.java:348)
    at java.desktop/javax.swing.JFrame.createRootPane(JFrame.java:279)
    at java.desktop/javax.swing.JFrame.frameInit(JFrame.java:258)
    at java.desktop/javax.swing.JFrame.<init>(JFrame.java:225)
    at Walkthrough.<init>(Walkthrough.java:7)
    at Walkthrough.lambda$main$0(Walkthrough.java:23)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel生活在radiance-substance-2.5-SNAPSHOT.jar中。我已经确认它确实存在jar tvf

jar tvf radiance-substance-2.5-SNAPSHOT.jar | grep SubstanceBusinessLookAndFeel
   548 Fri Feb 01 00:00:00 GMT 1980 org/pushingpixels/substance/api/skin/SubstanceBusinessLookAndFeel.class

So at this point I'm sure that I'm messing something up with the classpath, but I just don't know what. I'm on Fedora 28 and my java version is:

java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
提问于
用户回答回答于

那么这与类路径无关,而是与jar库本身有关。我发现默认情况下我的机器上的字体没有消除锯齿(Fedora 28,使用i3-gap作为窗口管理器)。我想知道这是否与某种程度相关,并对Java运行时环境字体进行了一些阅读。从本质上讲,字体看起来像这样:

但是用这个系统属性启动应用程序:-Dawt.useSystemAAFontSettings=on固定的东西:

事实证明,在使用Radiance启动Walkthrough时设置此属性使其工作:

java -Dawt.useSystemAAFontSettings=on -Dswing.defaultlaf=org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel -cp .:tmp/radiance-substance-2.5-SNAPSHOT.jar:tmp/radiance-trident-2.5-SNAPSHOT.jar:tmp/radiance-neon-2.5-SNAPSHOT.jar Walkthrough.

删除此属性会导致Java无法加载org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel的错误。

在这里阅读有更多信息。我假设因为我已经定制了我的Fedora 28设置(我不使用gnome)是我没有gnome-settings-daemon运行Java用来获取系统默认值的原因,这对Radiance来说是个问题?不确定。

我已经通知了Radiance开发人员。如果/当他提供反馈时我会更新这个答案。

扫码关注云+社区

领取腾讯云代金券