B4J Question Inline Java question

Ilya G.

Active Member
Licensed User
Longtime User
Please tell me how to use this java code? I get an error: java.lang.ClassNotFoundException: java$lang$HashingPassword

B4X:
    Dim j As JavaObject
    j.InitializeNewInstance("HashingPassword", Null)
    Dim s As String = j.RunMethod("convertStringToHash", Array("Password123"))
    Log(s)

B4X:
#If Java
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.util.Base64;

public class HashingPassword {
    public String convertStringToHash(String password) throws NoSuchAlgorithmException {
        return Base64.getEncoder().encodeToString(hashPassword(password.toCharArray()));
    }

    public byte[] hashPassword(final char[] password) throws NoSuchAlgorithmException {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        PBEKeySpec spec = new PBEKeySpec(password, "irsc67Y3j1B8".getBytes(), 24000, 32);
        try {
            SecretKey key = secretKeyFactory.generateSecret(spec);
            return key.getEncoded();
        } catch (InvalidKeySpecException e) {
            BA.Log("error");
            return "error".getBytes();
        }
    }
}
#End If
 

agraham

Expert
Licensed User
Longtime User
1) The class needs to be declared static.

2) You need to supply the full class name to InitializeNewInstance. It is your package name plus the name of the B4A module plus the class name.

3) You need to supply a constructor. I don't know why the Java default constructor fails but there it is!

B4X:
Sub Button1_Click
    'xui.MsgboxAsync("Hello World!", "B4X")
    Dim j As JavaObject
    j.InitializeNewInstance("b4j.example.main.HashingPassword", Null)
    Dim s As String = j.RunMethod("convertStringToHash", Array("Password123"))
    Log(s)
End Sub


#If Java
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.util.Base64;

public static class HashingPassword {
    public String convertStringToHash(String password) throws NoSuchAlgorithmException {
        return Base64.getEncoder().encodeToString(hashPassword(password.toCharArray()));
    }
       
        public HashingPassword() {
        }
       
    public byte[] hashPassword(final char[] password) throws NoSuchAlgorithmException {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        PBEKeySpec spec = new PBEKeySpec(password, "irsc67Y3j1B8".getBytes(), 24000, 32);
        try {
            SecretKey key = secretKeyFactory.generateSecret(spec);
            return key.getEncoded();
        } catch (InvalidKeySpecException e) {
            BA.Log("error");
            return "error".getBytes();
        }
    }
}
#End If
 
Upvote 1
Top