Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

Java - SImple ESCP template not found

Currently i want to print a report in dot matrix printer using this: https://blog.jocki.me/simple-escp/

Here is my project directory:

Preview

This is my GUI Jframe to testing it

public class Index extends javax.swing.JFrame {

    public Index() throws IOException, URISyntaxException {
        initComponents();
        
        Template template = new JsonTemplate(Thread.currentThread().
           getContextClassLoader().getResource("com/app/tests/template.json").toURI()
        );
    }
  ... 
}
  

Let's running it by:

public static void main(String args[]) {
        
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
       try {
          new Index().setVisible(true);
       } catch (IOException | URISyntaxException ex) {
          Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
       }
    });
    }

I got error: How to access those template.json ?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at com.app.views.Index.<init>(Index.java:31)
question from:https://stackoverflow.com/questions/66059527/java-simple-escp-template-not-found

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Also your files/resources should exists under tests/resources.

The folder structure should look like:

-src -- test --- resources --- your-custom-folder --- template.json

Then try:

AnyTestClass.class.getClassLoader().getResource("your-custom-folder/template.json")

or try the below: 

AnyTestClass.class.getClassLoader().getResourceAsStream("your-custom-folder/template.json")

If it is a class that is not a Test class, then your resources should be under src/main/resources/your-custom-folder/template.json

Then its Index.class.getClassLoader().getResource()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...