B4J Question call java class from B4J

js1234

Member
Licensed User
Longtime User
Hi

I'm totaly beginner......
How to call java class?

JAVA:
_____________________________
B4X:
import java.io.FileInputStream
import java.security.Key
import java.security.KeyStore
import java.security.PrivateKey
import java.security.Signature
import java.text.SimpleDateFormat
import java.util.Date
import org.apache.commons.codec.digest.DigestUtils

Public class zoi {
Public static void Main( ) { 
    String taxnumber = "12345678"
    String vmesni_rezultat = taxnumber;
    String IssueDateTime = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss" ).format(new Date())
    vmesni_rezultat = vmesni_rezultat + IssueDateTime
    String InvoiceNumber = "12345"
     vmesni_rezultat = vmesni_rezultat + InvoiceNumber
    String BusinessPremiseID = "blag001"
    vmesni_rezultat = vmesni_rezultat + BusinessPremiseID
    String ElectronicDeviceID = "11245"
    vmesni_rezultat = vmesni_rezultat + ElectronicDeviceID
    String InvoiceAmount = "1245.56"
     vmesni_rezultat = vmesni_rezultat + InvoiceAmount
    byte[] podpisano = Null
   
    Try {
         FileInputStream file_inputstream = new FileInputStream("clientcert.jks")
         KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
         keyStore.load(file_inputstream, "futest".toCharArray())
         String alias = "itm storitve\\, špela pergar s.p."
        Key privateKey = keyStore.getKey( alias, "futest".toCharArray())
        Signature podpis = Signature.getInstance("SHA256withRSA")
           podpis.initSign((PrivateKey)privateKey)
        podpis.update(vmesni_rezultat.getBytes())
        podpisano = podpis.sign()
      }  Catch ( Exception e ) {
       '// napaka / error 
       e.printStackTrace()
       }   
       '// zoi = izračunajMD5(elektronsko podpisan vmesni_rezultat) / zoi = claculateMD5(digitally signed //vmesni_rezultat) 
        String zoi = DigestUtils.md5Hex(podpisano)
      System.out.println("32-mestni ZOI je: " + zoi); 
     }
}
_____________________________
 

Daestrum

Expert
Licensed User
Longtime User
Just a note - in B4J you need the javaobject set to Me , as javaobject.InitializeContext is only for B4A

eg
B4X:
Dim inline As JavaObject
inline = Me
.....
        inline.RunMethod(".......",null)
.....

The java code goes between
#If Java
....
#End If

It's worth putting these in BEFORE copying the code in otherwise, as you see in your listing above, the IDE will find keywords and change their case. You will need to change them back to correct java case otherwise you will get lots of errors.

The classes you add should be static ie public static class zoi { ...
 
Last edited:
Upvote 0
Top