java.io.IOException:流已关闭,但仅当从jar运行时

时间:2018-10-30 14:00:44

标签: java

当您直接从类中运行程序时,该程序将完美运行。但是,当使用cmd jar命令将程序创建到jar中时,启动程序时总是会出现运行时错误,并且程序也永远永远陷入循环。抱歉,我不能告诉您匹配的内容,我对Java很陌生。

这是导致问题的方法:

private static void pinyin()
{
    try
    {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

        for (int i = 0; i < size; i++)
        {
            if (!lines[i].equals(""))
            {
                lines[i] += " ";
            }
            String[] part = lines[i].split("#");
            part[2] = "";
            for (int y = 0; y < part[0].length(); y++)
            {

                String st = PinyinHelper.toHanyuPinyinStringArray(part[0].charAt(y), format)[0];
                part[2] = part[2] + st + " ";

            }
            lines[i] = part[0] + "#" + part[1] + "#" + (part[2].charAt(0) + "").toUpperCase() + part[2].substring(1, part[2].length() - 1);
        }
    } catch (Exception e)
    {
        System.out.println("Runtime Error" + e);
    }
}

它似乎是引起错误的行String st = PinyinHelper.toHanyuPinyinStringArray(part[0].charAt(y), format)[0];。 当我从罐子里跑出来的时候,我得到的错误是:

java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at java.io.FilterInputStream.read(Unknown Source)
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at net.sourceforge.pinyin4j.ChineseToPinyinResource.initializeResource(Unknown Source)
    at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
    at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
    at net.sourceforge.pinyin4j.ChineseToPinyinResource$ChineseToPinyinResourceHolder.<clinit>(Unknown Source)
    at net.sourceforge.pinyin4j.ChineseToPinyinResource.getInstance(Unknown Source)
    at net.sourceforge.pinyin4j.PinyinHelper.getUnformattedHanyuPinyinStringArray(Unknown Source)
    at net.sourceforge.pinyin4j.PinyinHelper.getFormattedHanyuPinyinStringArray(Unknown Source)
    at net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(Unknown Source)
    at PinYin.pinyin(PinYin.java:101)
    at PinYin.main(PinYin.java:30)
    at Launch$5.actionPerformed(Launch.java:138)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
Runtime Errorjava.lang.NullPointerException
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(Unknown Source)
        at PinYin.pinyin(PinYin.java:108)
        at PinYin.main(PinYin.java:30)
        at Launch$5.actionPerformed(Launch.java:138)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

这就是我用来创建罐子的方法:

jar -cmf *.mf SortRandomDeletePin.jar *.class net\*

请您尽一切可能帮助我,我很沮丧。

还请注意,我的程序从另一个jar导入了一个类。

1 个答案:

答案 0 :(得分:0)

ChineseToPinyinResource的代码中,您可以看到类路径(*)中必须有两个文件,即/pinyindb/unicode_to_hanyu_pinyin.txt/pinyindb/multi_pinyin.txt

private void initializeResource() {
    try {
        final String resourceName = "/pinyindb/unicode_to_hanyu_pinyin.txt";
        final String resourceMultiName = "/pinyindb/multi_pinyin.txt";

        setUnicodeToHanyuPinyinTable(new Trie());
        getUnicodeToHanyuPinyinTable().load(ResourceHelper.getResourceInputStream(resourceName));

        getUnicodeToHanyuPinyinTable().loadMultiPinyin(ResourceHelper.getResourceInputStream(resourceMultiName));

        getUnicodeToHanyuPinyinTable().loadMultiPinyinExtend();

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

请确保在运行时这些文件及其正确路径在类路径中可用。

(*):ResouceHelper类执行ResourceHelper.class.getResourceAsStream(resourceName)