B4J Question External Jar - cups4j what is a constructor

codie01

Active Member
Licensed User
Longtime User
Good Day All,

I have made progress and the following code works a treat and get the list of printers, Thanks!

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631))
   
    Dim myPrinters As List

    myPrinters = Regex.Split(",",cups.RunMethod("getPrinters", Null))

However this does not:

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress))
   
    Log(cups.RunMethod("CupsPrinter.getJobs", Array("ALL",Null,True)))

I don't understand this at all what a constructor is, here is the definitions and below is the full error message, thanks again.

----------------------------------------------

Constructor Summary


CupsPrinter(java.net.URL printerURL, java.lang.String printerName, boolean isDefault)
Constructor

-----------------------------------------------
getJobs
public java.util.List<PrintJobAttributes> getJobs(WhichJobsEnumwhichJobs,
java.lang.Stringuser,
booleanmyJobs)
throws java.lang.Exception
Get a list of jobs
Parameters:
whichJobs - completed, not completed or all
user - requesting user (null will be translated to anonymous)
myJobs - boolean only jobs for requesting user or all jobs for this printer?
Returns:
job list
Throws:
java.lang.Exception

------------------------------------------------


java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:191)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:491)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:437)
at b4j.example.printgenero._page_parseevent(printgenero.java:365)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 10 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
... 17 more
Caused by: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:94)
at b4j.example.printgenero._getprintjobs(printgenero.java:289)
at b4j.example.printgenero._printjobs_clicked(printgenero.java:446)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 18 more
 

codie01

Active Member
Licensed User
Longtime User
Here is the constructor detail, now I understand but how to use in B4j i don't have a clue.

Constructor Detail

CupsPrinter
public CupsPrinter(java.net.URLprinterURL,
java.lang.StringprinterName,
booleanisDefault)
Constructor
Parameters:
printerURL -
printerName -
isDefault - true if this is the default printer on this IPP server
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
From the first part of your first post you are getting the printers available.
This is what you need to use to get the second part to work.

Once you have selected a printer from the list, you can use that to get it's jobs.

This code
B4X:
Log(cups.RunMethod("CupsPrinter.getJobs", Array("ALL",Null,True)))

will need to change to something like
B4X:
Dim printerWanted as JavaObject = myPrinters.get(0) ' for example the first printer
 
Log(printerWanted.RunMethod("getJobs",Array("ALL",Null,True)))
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Thanks DaeStrum for your help. This however your solution gives me the same error if I use the code below and I wish to return all print jobs:

Also still do not understand why the constructor is not used. Also here is the link to the api document: http://cups4j.org/api/docs/api/

Again thanks!

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631)) 'this works fine
   
    Log(cups.RunMethod("getJobs", Array("ALL",Null,True)))

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Method: getJobs not matched.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:191)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Method: getJobs not matched.
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:491)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:437)
at b4j.example.printgenero._page_parseevent(printgenero.java:365)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 10 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Method: getJobs not matched.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
... 17 more
Caused by: java.lang.RuntimeException: Method: getJobs not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
at b4j.example.printgenero._getprintjobs(printgenero.java:291)
at b4j.example.printgenero._printjobs_clicked(printgenero.java:446)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 18 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
java.util.List<PrintJobAttributes> getJobs(CupsPrinter printer, WhichJobsEnum whichJobs, java.lang.String userName, boolean myJobs)
Returns all jobs for given printer and user Name Currently all Jobs on the server are returned by this method.

The first argument must be a CupsPrinter but you are giving a String here.

I guess you need to first get the list of all Printers

B4X:
  java.util.List<CupsPrinter>     getPrinters()
          Returns all available printers

and then use the right printer-object with the call you are trying.
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Hi All, Thanks for your answers.

I have tired the following code and am getting a constructor not found error. Still don,t understand how to build and use a constructor in B4J, Really appreciate help!

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631))
  
   'Build the constructor
    Dim myCupsPrinter As JavaObject
    myCupsPrinter.InitializeNewInstance("org.cups4j.CupsPrinter",Array("000.000.000.000","front_labeler",False)) ' <-------- Errors out here
  
    Log(myCupsPrinter.RunMethod("getJobs", Array(myCupsPrinter,"ALL",Null,Null,True))) '  < ----------- Will this be correct?

This following code works well for getting the printers and all that is required for the constructor.

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631))
  
    Dim myPrinters As List

    myPrinters = Regex.Split(",",cups.RunMethod("getPrinters", Null))

The Full Error Returned in the log:


java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:191)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:491)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:437)
at b4j.example.printgenero._page_parseevent(printgenero.java:378)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 10 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:482)
... 17 more
Caused by: java.lang.RuntimeException: Constructor not found.
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:94)
at b4j.example.printgenero._getprintjobs(printgenero.java:308)
at b4j.example.printgenero._printjobs_clicked(printgenero.java:459)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 18 more
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Good Day Don, Thank you so much, I am now closer. I can see you have a donate button, thank you for your time as this has held me up for over a week.

The first part of the code returns an array list as shown log(myprinters)

(ArrayList) [printer uri=[URL]http://ip-104-238-86-123.ip.secureserver.net:631/printers/SDL-HP_OfficeJet_Pro_6960_PCL-3_Network[/URL] default=false name=SDL-HP_OfficeJet_Pro_6960_PCL-3_Network, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Clothing_front_labeler default=false name=SHR-Clothing_front_labeler, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Furniture_desktop_labeler default=false name=SHR-Furniture_desktop_labeler, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Furniture_laptop_labeler default=false name=SHR-Furniture_laptop_labeler, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Furniture_mobile_1 default=false name=SHR-Furniture_mobile_1, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Home_labeler default=false name=SHR-Home_labeler, printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SHR-Kids_back_room default=false name=SHR-Kid...

where as the constructor details is

Constructor Detail

CupsPrinter
public CupsPrinter(java.net.URLprinterURL,
java.lang.StringprinterName,
booleanisDefault)
Constructor
Parameters:
printerURL -
printerName -
isDefault - true if this is the default printer on this IPP server​

In the last log I get an error as Follows:

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Method: getJobs not matched.

Appreciate any ideas thanks.

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631))
    Dim myPrinters As List
    myPrinters = cups.RunMethod("getPrinters", Null)
   
    Log(myPrinters)
   
    Dim printerWanted As JavaObject = myPrinters.get(0) ' for example the first printer
    Log(printerWanted.RunMethod("getJobs",Array(printerWanted , "ALL",Null,True)))
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Maybe try
B4X:
Log(cups.RunMethod("getJobs",Array(printerWanted , "ALL",Null,True)))
' instead of
'Log(printerWanted.RunMethod("getJobs",Array(printerWanted , "ALL",Null,True)))
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
Thanks guys, the following code works without errors:

B4X:
    Dim cups As JavaObject
    cups.InitializeNewInstance("org.cups4j.CupsClient",Array(ABMShared.myServerAddress,631))
 
    Dim myPrinters As List
    myPrinters = cups.RunMethod("getPrinters", Null)
 
    Dim printerWanted As JavaObject = myPrinters.get(0) ' for example the first printer
    Log(cups.RunMethod("getJobs",Array(printerWanted , "ALL",Null,False)))

There is no result in the log and I get an error on the cups server:

E [01/Feb/2018:15:30:56 -0700] Request from "27.110.126.71" using invalid Host: field "ip-104-238-86-123.ip.secureserver.net:631"

The log for myprinters.get(0) is:

printer uri=http://ip-104-238-86-123.ip.secureserver.net:631/printers/SDL-HP_OfficeJet_Pro_6960_PCL-3_Network default=false name=SDL-HP_OfficeJet_Pro_6960_PCL-3_Network

So I then tried to create my own list item as per the constructor requirement like so:

B4X:
    Dim myPrinters As List
    myPrinter.Add("printer uri=http://104.238.86.123,SDL-HP_OfficeJet_Pro_6960_PCL-3_Network,default=false")
    Dim printerWanted As JavaObject = myPrinters.get(0) ' for example the first printer
    Log(cups.RunMethod("getJobs",Array(printerWanted , "ALL",Null,False)))

Now I am again getting getjobs Method error again.

Is the list item I am creating wrong?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could try GetType(..) on the item returned from the list to check it is in fact a cups printer object.

B4X:
Log(GetType(myPrinters.Get(0))

It may be it has been converted to a string, which is not what you want.
So you could also try
B4X:
Dim myPrinters As List
myPrinters.Initialize
myPrinters.AddAll(cups.RunMethod("getPrinters", Null))
 
Upvote 0

codie01

Active Member
Licensed User
Longtime User
I got it thanks guys. Used reflection. I have learned a lot after reading heaps on classes, constructors and methods. I am going to right up a tutorial. Where can I put it on B4

Thanks again Phil
 
Upvote 0
Top