ARTICLE AD BOX
I have a desktop application that needs to read data from a text file and display images from jpgs. Everything works fine in NETBEANS, and the JAR opens and runs correctly from the command line once I've moved it to a non-NETBEANS directory. However, when I open the app by double clicking, the GUI opens, but the fields that are supposed to display data are blank.
I have seen other posts that suggest using IOStreams rather than FileReaders and FileWriters, so I have re-written everything in this way. However, same thing happens - it all works in NETBEANS, it all works fine from the command line with java -jar <myfile.jar>, but double-clicking the <myfile.jar> icon simply displays my GUI without any of the data it is supposed to have read and without displaying any images.
I have also read a number of articles about CLASS-PATHS, but would have guessed that if this was the problem my app would not be able to run from the command line either? but it works fine.
Here is a simplified version of what I'm trying to do - read a .txt file and display it's contents in a JPanel (I realise that I have included more imports than I need for this simplified version):
package com.steve; import javax.swing.*; import java.awt.*; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.StandardCharsets; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class Testing extends JFrame{ public Testing(){ super("My Test App"); EntryPanel panel = new EntryPanel(); this.getContentPane().add(panel); this.setSize(800, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public class EntryPanel extends JPanel{ String strTextToDisplay; public EntryPanel(){ super(); try{ this.strTextToDisplay = getTextFromFile(); } catch(IOException ioe){ System.out.println(ioe.getMessage()); } } private String getTextFromFile() throws IOException{ String fileName = "testfile.txt"; var filePath = Paths.get(fileName); String myStr = "Here it is:\n"; try(BufferedReader br = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)){ String line; while((line = br.readLine()) != null){ myStr = myStr.concat(line).concat("\n"); } } System.out.println(myStr); return myStr; } @Override public void paintComponent(Graphics g){ g.setColor(new Color(0, 0, 255)); g.fillRect(0,0, this.getWidth(), this.getHeight()); g.setColor(new Color(255, 100, 100)); g.setFont(new Font("Serif", Font.BOLD, 15)); g.drawString(strTextToDisplay, 20, 20); } } public static void main(String[] args){ System.out.println("In main"); Testing myTest = new Testing(); } }My manifest.txt file is as follows:
Manifest-Version: 1.0
Class-Path: com.resources/testfile.txt
Main-Class: com.steve.Testing
Created-By: 21.0.10 (Ubuntu)
Grateful for any advice that anyone can give, it is so frustrating to have an App that does not work in isolation.
Thanks
