Search code examples
javaeclipsejarnullpointerexceptionfreetts

Java Program - Works in Eclipse but Not in JAR - FreeTTS


I have been creating a Java program and it runs perfectly in Eclipse with no errors at all. When I compile it into a .jar and run it I get this error:

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)

I completely sure that these errors are coming from FreeTTS, when I try to do voice.allocate(); (I surrounded the code with try/catch to make sure and it caught the exception there.) This is Speech.java, the class that causes the errors.

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();
            }
        }
    }
}

The errors are caused when either method is called, and the line of the error is always where voice.allocate(); is. How can I make my program work as a .jar file and what am I doing wrong?!


Solution

  • Assuming FreeTTS is a separate jar that you have a dependency on, you have two options:

    1. You can follow the answer here to put everything into one jar
    2. You'll have to specify the second jar on the classpath when you execute your jar. For example: java -cp .:path/to/your/jar/yourjar.jar:path/to/other/jar/FreeTTS.jar com.main.method.Class