Java Question Help with creating a JavaPOS B4J library.

Chris Guanzon

Active Member
Licensed User
Longtime User
Hello everyone, could someone please guide/help me on how to do this? I have the JavaPOS JAR file, and I want to create a library for B4J with it.
 

Chris Guanzon

Active Member
Licensed User
Longtime User
You can use Java Object else you will have to write a wrapper(s).

Hello Johan, thank you for your response. Could you assist/guide me on how to proceed? What steps do I need to take, or where should I begin, and how should I go about it? If you require the JavaPOS, I can upload it here for you to test.
 

Johan Schoeman

Expert
Licensed User
Longtime User
You will need to know some java regardless of using Java Object or doing a wrapper. Search for some b4J examples posted by @stevel05 . He has posted many B4J projects using Java Object. You will have to use some tool to open the jar so that you can see the classes and the public methods in each of the classes.

I am too tied up with work to take on something like this. Maybe post a job offer in the job offer part of the forum and see if someone will assist you.
 

Chris Guanzon

Active Member
Licensed User
Longtime User
You will need to know some java regardless of using Java Object or doing a wrapper. Search for some b4J examples posted by @stevel05 . He has posted many B4J projects using Java Object. You will have to use some tool to open the jar so that you can see the classes and the public methods in each of the classes.

I am too tied up with work to take on something like this. Maybe post a job offer in the job offer part of the forum and see if someone will assist you.

Thanks Johan. Is there a tutorial for JavaObject for B4J here in the forum? I can't seem to find a comprehensive guide on how to do it.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks Johan. Is there a tutorial for JavaObject for B4J here in the forum? I can't seem to find a comprehensive guide on how to do it.
I dont think there is a guide but there are plenty of examples in the B4J forum. As mentioned before, you will need to have some knowledge of Java to use JavaObject or to do a B4J wrapper.
 

aeric

Expert
Licensed User
Longtime User
Is there a tutorial for JavaObject for B4J here in the forum?
It is similar in B4J when using JavaObject in B4A.

Check how to use Inline Java code.

 

Chris Guanzon

Active Member
Licensed User
Longtime User
Hello, I am trying to create a sample library for javaPOS, but I am having an error message. This is the error message.

B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
java.lang.NoClassDefFoundError: jpos/JposException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:340)
    at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:519)
    at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:506)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:252)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at b4j.example.main.<clinit>(main.java:17)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:427)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.loadClass(LauncherImpl.java:382)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$3(LauncherImpl.java:421)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassNotFoundException: jpos.JposException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 25 more
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.NullPointerException
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:458)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    ... 5 more
java.lang.ExceptionInInitializerError
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:427)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.loadClass(LauncherImpl.java:382)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$3(LauncherImpl.java:421)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: jpos/JposException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:140)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at b4j.example.main.<clinit>(main.java:17)
    ... 12 more
Caused by: java.lang.NoClassDefFoundError: jpos/JposException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:340)
    at anywheresoftware.b4a.shell.Shell.getCorrectClassName(Shell.java:519)
    at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:506)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:252)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    ... 14 more
Caused by: java.lang.ClassNotFoundException: jpos.JposException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 25 more

jposException is added in my reference library.

1713014405331.png


What do you think my mistake is here?

This is the java code.


Java:
package anywheresoftware.b4j.javaPOS;

import anywheresoftware.b4a.BA.ShortName;

import jpos.JposConst;
import jpos.JposException;
import jpos.POSPrinter;
import jpos.POSPrinterConst;

//import jpos.events.ErrorEvent;
//import jpos.events.ErrorListener;
//import jpos.events.OutputCompleteEvent;
//import jpos.events.OutputCompleteListener;
//import jpos.events.StatusUpdateEvent;
//import jpos.events.StatusUpdateListener;
import jpos.util.JposPropertiesConst;

@ShortName("javaPOS")
public class javaPOS {
    /**
     *
     * set the printer name.
     * USB = JposPrinter_USB
     * SERIAL = JposPrinter_serial
     */
    public void PrintTest(){
        /*
         * If you want to place the jpos.xml file elsewhere on your local file
         * system then uncomment the following line and specify the full path to
         * jpos.xml.
         */
        //System.setProperty(    JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME, "jpos.xml");
        System.setProperty(JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME, "c:/SDK/javaPOS/JavaPOS/JposPrinterJavaPOS_Beta04/pos.xml");

        String ESC = ((char) 0x1b) + "";
        String LF = ((char) 0x0a) + "";
        String QR_DOT=((char)0x8)+"";
        String QR_ER_LV=((char)0x30)+"";
        String PDF_PAR=((char)0x00)+""+((char)0x00)+""+((char)0x06)+""+((char)0x03)+""+
                ((char)0x30)+""+((char)0x32)+""+((char)0x00)+"";
        String SPACES = "                                                                      ";
        
        // Create an instance of the POSPrinter
        POSPrinter printer = new POSPrinter();

        try {
            // register for asynchronous OutputCompleteEvent notification
            //printer.addOutputCompleteListener(this);
            // Open the device
            printer.open("JposPrinter_USB");
            
            // claim exclusive usage of the printer object
             printer.claim(1);
            
             // enable the device for input and output
             printer.setDeviceEnabled(true);
            
             printer.setAsyncMode(true);
            
             // set map mode to metric - all dimensions specified in 1/100mm units
             printer.setMapMode(POSPrinterConst.PTR_MM_METRIC); // unit = 1/100 mm - i.e. 1 cm = 10 mm = 10 * 100 units
            
             do {
                 // register for asynchronous StatusUpdateEvent notification
                // see the JavaPOS specification for details on this

                // printer.checkHealth(JposConst.JPOS_CH_EXTERNAL);
                // printer.checkHealth(JposConst.JPOS_CH_INTERACTIVE);

                // check if the cover is open
                if (printer.getCoverOpen() == true) {
                    System.out.println("printer.getCoverOpen() == true");

                    // cover open so do not attempt printing
                    break;
                }
                
                // check if the printer is out of paper
                if (printer.getRecEmpty() == true) {
                    System.out.println("printer.getRecEmpty() == true");

                    // the printer is out of paper so do not attempt printing
                    break;
                }
                
                // being a transaction
                // transaction mode causes all output to be buffered
                // once transaction mode is terminated, the buffered data is
                // outputted to the printer in one shot - increased reliability
                printer.transactionPrint(POSPrinterConst.PTR_S_RECEIPT, POSPrinterConst.PTR_TP_TRANSACTION);

                if (printer.getCapRecBitmap() == true) {
                    // print an image file
                    try {
                        printer.printBitmap(POSPrinterConst.PTR_S_RECEIPT, "posprinter.jpg", POSPrinterConst.PTR_BM_ASIS, POSPrinterConst.PTR_BM_CENTER);
                    } catch (JposException e) {
                        if (e.getErrorCode() != JposConst.JPOS_E_NOEXIST) {
                            // error other than file not exist - propogate it
                            throw e;
                        }
                        // image file not found - ignore this error & proceed
                    }
                }

                // call printNormal repeatedly to generate out receipt the following
                // JavaPOS-POSPrinter control code sequences are used here
                // ESC + "|cA" -> center alignment
                // ESC + "|4C" -> double high double wide character printing
                // ESC + "|bC" -> bold character printing
                // ESC + "|uC" -> underline character printing
                // ESC + "|rA" -> right alignment
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|cA" + ESC + "|bC" + "ZhuHai, China, " + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|cA" + ESC + "|bC" + "0756-3895710" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|uC" + "Qnty Unit Tx Description" +
                        SPACES.substring(0, printer.getRecLineChars() - "Qnty Unit Tx Description".length()) + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, "   1  100    POS58IIH" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, "   1  200    POS76E" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, "   1  300    POSC2008" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + "Subtotal:  600" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + "Tax:        50" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + ESC + "|bC" + "Total:     650" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + "Tender:    700" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|rA" + ESC + "|bC" + "Change:      50" + LF);
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, LF);

                if (printer.getCapRecBarCode() == true) {
                    // print a PDF417 Code
                    // the 10 * 100, 60 * 100 parameters below specify the barcode's
                    // height and width in the metric map mode (1cm tall, 6cm wide)
                    /*printer.printBarCode(POSPrinterConst.PTR_S_RECEIPT,
                            ESC+PDF_PAR+"JposPrinter(C)2016 By Lee",
                            POSPrinterConst.PTR_BCS_PDF417,
                            10 * 100, 60 * 100,
                            POSPrinterConst.PTR_BC_CENTER,
                            POSPrinterConst.PTR_BC_TEXT_NONE);*/
                    printer.printBarCode(POSPrinterConst.PTR_S_RECEIPT,
                    ESC+QR_DOT+QR_ER_LV+"JposPrinter(C)2016 By Lee",
                    POSPrinterConst.PTR_BCS_QRCODE,
                    10 * 100, 60 * 100,
                    POSPrinterConst.PTR_BC_CENTER,
                  POSPrinterConst.PTR_BC_TEXT_NONE);
                    /*printer.printBarCode(POSPrinterConst.PTR_S_RECEIPT,
                            "JposPrinter(C)2016 By Lee",
                            POSPrinterConst.PTR_BCS_Code128,
                            10 * 100, 60 * 100,
                            POSPrinterConst.PTR_BC_CENTER,
                            POSPrinterConst.PTR_BC_TEXT_NONE);*/
                }

                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|cA" + ESC + "|4C" + ESC + "|bC" + "Thank you" + LF);

                // the ESC + "|100fP" control code causes the printer to execute
                // a paper cut after feeding to the cutter position
                printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, ESC + "|100fP");

                // terminate the transaction causing all of the above buffered
                // data to be sent to the printer
                printer.transactionPrint(POSPrinterConst.PTR_S_RECEIPT, POSPrinterConst.PTR_TP_NORMAL);

                System.out.println("Async transaction print submited: time = "
                        + System.currentTimeMillis() + " output id = " + printer.getOutputID());

                // exit our printing loop
             } while (false);
            
        } catch (Exception e) {
            e.printStackTrace();
            
        } finally {
            // close the printer object
            if (printer.getState() != JposConst.JPOS_S_CLOSED) {
                try {
                    while (printer.getState() != JposConst.JPOS_S_IDLE) {
                        Thread.sleep(0);
                    }

                    printer.close();
                } catch (Exception e) {
                }
            }
        }

    }
}
 

Chris Guanzon

Active Member
Licensed User
Longtime User
Are you insisting to use JavaPOS?
Have you tried using ESC/POS command to print on thermal printer?


Hello Aeric, are you referring to this? But this is a B4A library, not a B4J library. Is it compatible with B4J?
 

agraham

Expert
Licensed User
Longtime User
The EscPosPrinter.bas code module should be fairly easy to adapt to however your printer is connected in B4J. All the printer output is through two Subs in the module, WriteBytes and WriteString2, so you just need to modify them to send the data to your printer.
 

aeric

Expert
Licensed User
Longtime User
Yes, ESC/POS printer code module provided by agraham above can be use for B4A, B4J and B4i to print through Bluetooth, WiFi, USB and Serial. A little modification is required.
 

Chris Guanzon

Active Member
Licensed User
Longtime User
Yes, ESC/POS printer code module provided by agraham above can be use for B4A, B4J and B4i to print through Bluetooth, WiFi, USB and Serial. A little modification is required.

Hello Aeric, the ESC/POS printer code module provided by agraham doesn't include the code for the cash drawer. Is it possible to add this, or is there a tutorial here in the forum on how to do it?
 

DonManfred

Expert
Licensed User
Longtime User
Is it possible to add this, or is there a tutorial here in the forum on how to do it?
The Code is provided as a Class. Open it in the IDE and extend it by yourself. It is B4X-Code.
Check the cashdrawer documentation on how it should work.
 

aeric

Expert
Licensed User
Longtime User
You may need to check the "printer" documentation on the correct ESC/POS command to kick the drawer using the RJ11 cable.

My printer has slightly different command.

Then send the command to printBytes.
 

Chris Guanzon

Active Member
Licensed User
Longtime User
You may need to check the "printer" documentation on the correct ESC/POS command to kick the drawer using the RJ11 cable.

My printer has slightly different command.

Then send the command to printBytes.


I found this sample code in the forum, and it works fine. However, this code doesn't include the fontSize option like in the ESC/POS library by agraham. I tried editing the ESC/POS code using the code above, but I'm unsure how to do it. I can't connect the ESC/POS printer via USB like in the code above. How can I connect the ESC/POS printer to USB, similar to the code above?
 
Top