簡體   English   中英

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

[英]Java Program - Works in Eclipse but Not in JAR - FreeTTS

我一直在創建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. 您可以按照此處的答案將所有內容放入一個罐子中
  2. 執行jar時,必須在類路徑上指定第二個jar。 例如: java -cp .:path/to/your/jar/yourjar.jar:path/to/other/jar/FreeTTS.jar com.main.method.Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM