gpt4 book ai didi

java - 运行 JAR 文件时右键菜单损坏

转载 作者:行者123 更新时间:2023-12-01 13:52:43 26 4
gpt4 key购买 nike

我有一个 Java 应用程序,它在系统托盘中放置一个带有右键菜单的图标。当我从 IDE (NetBeans) 运行它时,它可以正常工作。但是当我构建它并运行 JAR 文件时,它会将图标放入系统托盘中,但当我右键单击它时,它没有上下文菜单。

这是一些代码。任何帮助表示赞赏。我检查了Java的平台版本和控制台版本,它们似乎都是相同的。

package abaxis;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Systray
{
private TrayIcon trayIcon = null;
private Image databaseimage;
private Image databaseDisconnectedImage;
public PopupMenu popup;
private ActionListener exitListener;
private ActionListener settingsListener;
private ActionListener connectListener;
private ActionListener disconnectListener;
private static Systray instance;
private static Abaxis abaxis = null;

private SystemTray tray;

private MenuItem connectItem;
private MenuItem settingsItem;
private MenuItem exitItem;
private MenuItem disconnectItem;

public Systray()
{
// create a action listener to listen for default action executed on the tray icon
exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
settingsListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//New Jframe.
JFrame settingsJFrame = new AbaxisSettingsForm();
settingsJFrame.setVisible(true);
}
};
connectListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Start the application.
abaxis.connect();
setConnectedItems();
}
};
disconnectListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Discconect the application.
abaxis.disconnect();
setDisconnectedItems();
}
};
}

public static Systray getSystray()
{
if(instance == null)
{
instance = new Systray();
instance.init();
}
return instance;
}

public void init()
{
// get the SystemTray instance
tray = SystemTray.getSystemTray();
// load Images
databaseimage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/database.png"));
databaseDisconnectedImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/database_delete.png"));
// create a popup menu
this.popup = new PopupMenu();

// create menu item for the default action
connectItem = new MenuItem("Connect");
connectItem.addActionListener(connectListener);
settingsItem = new MenuItem("Settings");
settingsItem.addActionListener(settingsListener);
exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
disconnectItem = new MenuItem("Disconnect");
disconnectItem.addActionListener(disconnectListener);

// set the TrayIcon properties
//trayIcon.addActionListener(exitListener);
// construct a TrayIcon
trayIcon = new TrayIcon( databaseimage, "Abaxis", this.popup );
// add the tray image
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.err.println(e);
}

try
{
abaxis = Abaxis.start();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

if(abaxis.isConnected())
{
//Set all the stuff if its connected.
setConnectedItems();
}
else
{
//Set all the stuff if its disconnected.
setDisconnectedItems();
}
}

private void setConnectedItems()
{
this.popup.removeAll();
this.popup.add(disconnectItem);
this.popup.add(settingsItem);
this.popup.add(exitItem);
this.updateImage(databaseimage);

}

private void setDisconnectedItems()
{
this.popup.removeAll();
this.popup.add(connectItem);
this.popup.add(settingsItem);
this.popup.add(exitItem);
this.updateImage(databaseDisconnectedImage);
}

public void updateImage(Image x)
{
if (this.trayIcon != null)
{
this.trayIcon.setImage(x);
}
}

}

谢谢,我设法发现这是一个丢失 jar 的问题。但 jar 文件似乎位于 lib 文件夹中。所以我不知道为什么它有一个翅膀。堆栈跟踪:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
at abaxis.Abaxis.connectToBloodMachine(Abaxis.java:114)
at abaxis.Abaxis.connect(Abaxis.java:90)
at abaxis.Abaxis.init(Abaxis.java:76)
at abaxis.Abaxis.start(Abaxis.java:53)
at abaxis.Systray.init(Systray.java:141)
at abaxis.Systray.getSystray(Systray.java:88)
at abaxis.Abaxis.main(Abaxis.java:279)

最佳答案

RXTX 与依赖于其运行的操作系统的驱动程序配合使用。这些驱动程序随 RXTX 库一起提供,它们是:

  • librxtxSerial.solibrxtxParallel.so(适用于 Linux)。
  • librxtxSerial.dlllibrxtxParallel.dll(适用于 Windows)。
  • librxtxSerial.jnilib 适用于 Mac

This link有正确安装这些驱动程序的说明和这个问题:java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path有几种解决方法可以解决此类异常。

但是,它们都假设您有权访问 %JAVA_HOME% 路径,或者您知道驱动程序所在的位置,如果您在计算机上工作,这很好,但如果您需要分发应用程序,则不行。如果是这种情况,只需将驱动程序文件复制到放置 jar 的同一文件夹中即可。您必须看到类似这样的内容才能知道它正常工作:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7

更新

最近我注意到 Linux 的 java.library.path 属性中不包含当前工作目录。因此,如果您只是将 librxtxSerial.so 添加到应用程序文件夹中,您仍然会收到 UnsatisfiedLinkError。再次,您可以在通过命令行运行应用程序时设置此属性,如下所示:

java -jar -Djava.library.path=/path/to/lib

或者您可以使用此处解释的 hacky 方法:Setting "java.library.path" programmatically 。这种方法不安全,如果您有权实现另一种解决方法,我不建议您使用它,但我已经尝试过并且它有效:

String javaLibraryPath = System.getProperty("java.library.path");

if(!javaLibraryPath.contains(File.pathSeparator + "." + File.pathSeparator)){

StringBuilder sb = new StringBuilder(javaLibraryPath).append(File.pathSeparator).append(".");
// The line above appends "." (current directory) to the library path
// Of course 'rxtxSerial.so' must be placed in the current directory

System.setProperty("java.library.path", sb.toString());

try {
Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);// Setting this field to null will force a reevaluation of
// the library path as soon as loadLibrary() is called

} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ex.printStackTrace();
}
}

关于java - 运行 JAR 文件时右键菜单损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19849558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com