Java程序不能显示Jar文本文件的内容 [英] Java Program Not Showing Contents of Text File in Jar

查看:124
本文介绍了Java程序不能显示Jar文本文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个java项目,并且在主屏幕上显示项目中的文本文件的内容。这在Eclipse中工作正常,但导出到可运行jar文件时不起作用。这里是什么在Eclipse中看起来像:

以下是在可运行Jar文件中的样子:


这里是代码:

 

code> import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class HomeFrame {

static JPanel panel;
public static JPanel p2;
private static JButton play = new JButton(Play);
File patch = new File(Main.class.getResource(/ counter / res / ResourceCounterPatchNotes.txt)。getFile());
私人静态JButton微博;
私有静态JButton静音;
私人静态JButton信息;
私有静态JButton themeChooser;
私有静态JLabel stoneLogs;

// private static JLabel text;
public static JLabel greet = new JLabel(,SwingConstants.CENTER);

static JFrame frame = new JFrame(Resource Counter - Home(by Grayson S)); {
frame.setSize(800,520);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.repaint();
frame.revalidate();

createView();
}

private void createView(){
setIcon();

panel = new JPanel();
frame.getContentPane()。add(panel);
play.setPreferredSize(new Dimension(200,70));
p2 = new JPanel();
frame.getContentPane()。add(p2);
p2.setLayout(new GridBagLayout());

play.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
try {
Thread.sleep(500 );
} catch(InterruptedException ex){
Thread.currentThread()。interrupt();
}
SelectionFrame.frame1.setVisible(true);
frame .setVisible(false);
}

});

p2.setLayout(new GridBagLayout());
p2.setBackground(Color.BLACK); //设置背景颜色
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,8,0,0);

JTextArea ta = new JTextArea(10,20);
ta.setForeground(Color.WHITE);
ta.setBackground(Color.BLACK);
ta.setFont(new Font(Lucida Sans,Font.PLAIN,12));
//ta.setMargin(new Insets(12,12,12,12));
p2.add(ta,gbc);
//gbc.fill = GridBagConstraints.VERTICAL;
play.setFont(new Font(Segoe UI,Font.BOLD,16));

gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8,8,0,5);
gbc.anchor = GridBagConstraints.NORTH;
p2.add(info = new JButton(Info),gbc);
info.setMargin(new Insets(12,12,12,12));
gbc.gridy = 1;
p2.add(themeChooser = new JButton(Theme Chooser),gbc);
themeChooser.setMargin(new Insets(12,12,12,12));
gbc.gridy = 2;
p2.add(twitter = new JButton(Grayson's Twitter),gbc);
twitter.setMargin(新的Insets(12,12,12,12));
gbc.gridy = 3;
p2.add(mute = new JButton(Mute Music),gbc);
mute.setMargin(new Insets(12,12,12,12));
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.CENTER;
p2.add(stoneLogs = new JLabel(),gbc);
gbc.gridy = 5;

Image img = new ImageIcon(this.getClass()。getResource(/ counter / res / Pick2.png))。getImage();
stoneLogs.setIcon(new ImageIcon(img));

themeChooser.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
ThemeFrame.frame.setLocationRelativeTo(null);
ThemeFrame.frame.setVisible(true);
}

});

info.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
InfoFrame.frame.setLocationRelativeTo(null);
InfoFrame.frame.setVisible(true);
}

});

mute.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if(Main.clip.isRunning()){
Main.clip.stop();
mute.setText(Play Music);
} else {
Main.clip.loop(Clip.LOOP_CONTINUOUSLY);
Main.clip.start();
mute.setText(Mute Music);
}
{
}
}

});

twitter.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
try {

String URL =https://twitter.com/Graysull;
java.awt.Desktop.getDesktop()。browse(java.net.URI.create(URL));

catch(IOException e1){
e1.printStackTrace();
}

}});

gbc.gridx = 0;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(play,BorderLayout.SOUTH);

frame.getContentPane()。setBackground(Color.GRAY);

尝试{
ta.read(new FileReader(patch),null);
ta.setEditable(false);
} catch(FileNotFoundException e1){
e1.printStackTrace();
} catch(IOException e1){
e1.printStackTrace();


$ b greet.setFont(new Font(Dialog,Font.BOLD,20));
frame.getContentPane()。add(greet,BorderLayout.NORTH);


$ b public void setIcon(){
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource(/ counter / res /Pick.png)));






我该如何解决这个问题?帮助表示赞赏。

解决方案文件仅用于文件系统,不在罐子里面;您需要使用资源: Class,getResource Class,getResourceAsStream



FileReader 是一个使用默认平台编码的工具类。但是你可能会在另一台计算机上部署该jar,然后编码可能会有所不同。



最好保留UTF-8或ISO-8859-1(拉丁文-1)。那些总是可用的。 (b




$ .class.getResourceAsStream(
/counter/res/ResourceCounterPatchNotes.txt),
StandardCharsets.UTF_8))){
ta.read(in);
}

获取NullPointerException时,路径可能从错误的目录开始,路径不符合wrt案子。要检查路径,请打开jar(例如7zip或WinZip)。


I have created a java project and on the home screen it is supposed to show the contents of a text file within the project. This works fine in Eclipse, but does not work when exported to a runnable jar file. Here is what is looks like in Eclipse:

Here is what it looks like in a runnable Jar file:

Here is the code:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class HomeFrame {

static JPanel panel;
public static JPanel p2;
private static JButton play = new JButton("Play");
File patch = new File(Main.class.getResource("/counter/res/ResourceCounterPatchNotes.txt").getFile());
private static JButton twitter;
private static JButton mute;
private static JButton info;
private static JButton themeChooser;
private static JLabel stoneLogs;

//private static JLabel text;
public static JLabel greet = new JLabel("", SwingConstants.CENTER);

static JFrame frame = new JFrame("Resource Counter - Home (by Grayson S)"); {
frame.setSize(800, 520);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.repaint();
frame.revalidate();

createView();
}

private void createView() {
setIcon();

panel = new JPanel();
frame.getContentPane().add(panel);
play.setPreferredSize(new Dimension(200, 70));
p2 = new JPanel();
frame.getContentPane().add(p2);
p2.setLayout(new GridBagLayout());  

play.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Thread.sleep(500);   
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        SelectionFrame.frame1.setVisible(true);
        frame.setVisible(false);
    }

});

p2.setLayout(new GridBagLayout());
p2.setBackground(Color.BLACK); //sets the background color.
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 8, 0, 0);

JTextArea ta = new JTextArea(10, 20);
ta.setForeground(Color.WHITE);
ta.setBackground(Color.BLACK);
ta.setFont(new Font("Lucida Sans", Font.PLAIN, 12));
//ta.setMargin(new Insets(12, 12, 12, 12));  
p2.add(ta, gbc);
//gbc.fill = GridBagConstraints.VERTICAL;
play.setFont(new Font("Segoe UI", Font.BOLD, 16));

gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8, 8, 0, 5);
gbc.anchor = GridBagConstraints.NORTH;
p2.add(info = new JButton("Info"), gbc);
info.setMargin(new Insets(12, 12, 12, 12)); 
gbc.gridy = 1;
p2.add(themeChooser = new JButton("Theme Chooser"), gbc);
themeChooser.setMargin(new Insets(12, 12, 12, 12));
gbc.gridy = 2;
p2.add(twitter = new JButton("Grayson's Twitter"), gbc);
twitter.setMargin(new Insets(12, 12, 12, 12));
gbc.gridy = 3;
p2.add(mute = new JButton("Mute Music"), gbc);
mute.setMargin(new Insets(12, 12, 12, 12));
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.CENTER;
p2.add(stoneLogs = new JLabel(""), gbc);
gbc.gridy = 5;

Image img = new ImageIcon(this.getClass().getResource("/counter/res/Pick2.png")).getImage();
stoneLogs.setIcon(new ImageIcon(img));

themeChooser.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        ThemeFrame.frame.setLocationRelativeTo(null);
        ThemeFrame.frame.setVisible(true);
    }

});

info.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        InfoFrame.frame.setLocationRelativeTo(null);
        InfoFrame.frame.setVisible(true);
    }

});

mute.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (Main.clip.isRunning()) {
            Main.clip.stop();
            mute.setText("Play Music");
        } else {
            Main.clip.loop(Clip.LOOP_CONTINUOUSLY);
            Main.clip.start();
            mute.setText("Mute Music");
        }
        {
        }
    }

});

twitter.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {

            String URL = "https://twitter.com/Graysull";
        java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));

        } catch (IOException e1) {
            e1.printStackTrace();
    }

}});

gbc.gridx = 0;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(play, BorderLayout.SOUTH);

frame.getContentPane().setBackground(Color.GRAY);

try {
    ta.read(new FileReader(patch), null);
    ta.setEditable(false);
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}


greet.setFont(new Font( "Dialog", Font.BOLD, 20));
frame.getContentPane().add(greet, BorderLayout.NORTH);
}


public void setIcon() {
   frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/counter/res/Pick.png")));
}
}

How can I fix this? Help is appreciated.

解决方案

File is just for the file system, not inside a jar; you need to use the resource: Class,getResource or Class,getResourceAsStream,

Also FileReader is a utility class using the default platform encoding. But you might deploy the jar on another computer, and then the encoding could be different.

Best to keep the text in UTF-8 or ISO-8859-1 (Latin-1). Those are always available. And explicitly mention the encoding.

try (Reader in = new BufferedReader(
    new InputStreamReader(
        Main.class.getResourceAsStream(
            "/counter/res/ResourceCounterPatchNotes.txt"),
        StandardCharsets.UTF_8))) {
    ta.read(in);
}

When getting a NullPointerException, the path probably starts in the wrong directory, or the path does not match w.r.t. the case. To check the path, open the jar (with for instance 7zip or WinZip).

这篇关于Java程序不能显示Jar文本文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆