Java Question Getting IllegalAccessException on first attempt to create Java library...

Lee Gillie CCP

Active Member
Licensed User
Longtime User
First attempt to create a Java library for B4A. I must have missed something... ???

Just the one class in the library so far. Trying to pound out the mechanics to calling my library from B4A. But not too much luck so far. I'm guessing this is trivial to you who have already done this so many times successfully.

Here is the beginning of the class...

B4X:
package com.odp.odpcloud;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.Callable;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

import anywheresoftware.b4a.*;
import anywheresoftware.b4a.BA.*;

@ShortName("odp_Cloud")
@Version(0.1f)
@Author("Lee D. Gillie, CCP")
@Events( values = {
        "connected(Success as Boolean)"
        })
public class CloudConsumer {

    private BA ba;
    private String eventName;
    private boolean connected;
    private Socket socket;
    private String transport;
    
    
    CloudConsumer() {
        connected = false;
        socket = null;
        transport = null;
    }
    
    public void Initialize(BA ba, String EventName ) {
        this.ba = ba;
        this.eventName = EventName.toLowerCase(BA.cul);
    }
    
    public void connect(final String Hostname, final int Port, final int TimeoutMilliseconds) {
        BA.runAsync(ba, this, eventName + "_connected", new Object[] {false}, new Callable<Object[]>() {
               @Override
               public Object[] call() throws Exception {

                   socket = new Socket();
                   try {
                       InetAddress Address = InetAddress.getByName(Hostname);
                       InetSocketAddress i = new InetSocketAddress(Address, Port);
                       BA.Log("CloudConsumer connecting to " + Hostname + ":" + Integer.toString(Port));
                       socket.connect(i, TimeoutMilliseconds);
                       connected = true;
                       BA.Log("CloudConsumer connected.");

Getting this result....

*** Service (starter) Create ***
** Service (starter) Start **
Error occurred on line: 20 (Starter)
java.lang.IllegalAccessException: com.odp.odpcloud.CloudConsumer() is not accessible from class anywheresoftware.b4a.shell.Shell
at java.lang.Class.newInstance(Class.java:1569)
at anywheresoftware.b4a.shell.Shell.createObject(Shell.java:591)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.main.initializeGlobals(main.java:312)
at b4a.example.main.afterFirstLayout(main.java:99)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

My calling B4A code is... (note: I tried putting my cld declaration in both Globals and Process_Globals. That doesn't seem to affect anything, at least as far as the exception I am seeing.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private btnTest As Button
    Public cld As odp_Cloud
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("CloudTestLayout")
End Sub


Sub cld_connected(success As Boolean)
    Log("cld_connected(success=" & success & ")")
    
    Log("Closing...")
    cld.close()
    
End Sub

Sub btnTest_Click
    Log("Initializing...")
    cld.Initialize("cld")

    Log("Connecting...")
    cld.connect("FLASH.ODP.COM",1234,10000)
    
    Log("Awaiting connected event....")
        
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Moved to the Libraries developers forum.

Add 'public' before the constructor or just remove it as it isn't doing anything useful anyway.
Spot on! Thanks for the hint. Everything came to life. I'm experiencing Frankenstein joy now.
 
Top