gpt4 book ai didi

Java 程序 - 在 Eclipse 中工作,但在 JAR 中不工作 - FreeTTS

转载 作者:行者123 更新时间:2023-11-30 08:15:56 24 4
gpt4 key购买 nike

我一直在创建一个Java程序,它在Eclipse中完美运行,没有任何错误。当我将其编译成 .jar 并运行它时,出现以下错误:

java.lang.NullPointerException
at javaVoice.Speech.say(Speech.java:12)
at javaVoice.Respond.toText(Respond.java:58)
at javaVoice.GUI$2.actionPerformed(GUI.java:85)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)

at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

当我尝试执行 voice.allocate(); 时,我完全确定这些错误来自 FreeTTS; (我用 try/catch 包围了代码以确保它捕获了那里的异常。)这是 Speech.java,导致错误的类。

package javaVoice;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class Speech {
public static void say(String toSay) {
try {
Voice voice;
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice(Main.speakVoice);
voice.allocate();
voice.speak(toSay);
}
catch (Exception e) {
System.out.println("Something went wrong while javaVoice tried to talk!");
if (Main.debugMode) {
e.printStackTrace();
}
}
}
public static void sayPrint(String toSay) {
try {
Voice voice;
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice(Main.speakVoice);
voice.allocate();
voice.speak(toSay);
System.out.println(toSay);
}
catch (Exception e) {
System.out.println("Something went wrong while javaVoice tried to talk!");
if (Main.debugMode) {
e.printStackTrace();
}
}
}
}

调用任一方法都会导致错误,并且错误行始终位于 voice.allocate(); 处。是。如何使我的程序作为 .jar 文件运行?我做错了什么?!

最佳答案

假设 FreeTTS 是一个您依赖的单独的 jar,您有两个选择:

  1. 您可以关注答案here将所有东西放入一个 jar 中
  2. 当您执行 jar 时,您必须在类路径上指定第二个 jar。例如:java -cp .:path/to/your/jar/yourjar.jar:path/to/other/jar/FreeTTS.jar com.main.method.Class

关于Java 程序 - 在 Eclipse 中工作,但在 JAR 中不工作 - FreeTTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29678265/

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