B4J Tutorial Non-UI Applications

Discussion in 'B4J Tutorials' started by Erel, Nov 19, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    B4J currently supports two types of applications: UI applications (based on JavaFX) and Non-UI applications.

    Non-UI applications are standard Java applications. Usually the input to such applications is with command line arguments.

    You should create a new project and select:

    [​IMG]

    The program starts with:
    Code:
    Sub AppStart(Args() As String)

    End Sub
    The program will end after this sub unless you call StartMessageLoop. Calling StartMessageLoop means that the main thread is now handling an internal message queue. This is useful if you need to wait for some other event to happen.

    For example, we will create a very simple "curl" application. curl is a Linux app that allows you to send Http requests.

    In this case the program will receive one argument which is the URL, it will download it and print the downloaded resource.

    In order to test it we need to provide the command line argument. This is done with the #CommandLineArgs module attribute.

    The complete code:
    Code:
    'Non-UI application (console application)
    #Region  Project Attributes
       
    #CommandLineArgs: http://www.basic4ppc.com
    #End Region

    Sub Process_Globals
      
    End Sub

    Sub AppStart (Args() As String)
       
    If Args.Length = 0 Then
         
    Log("URL is missing.")
         ExitApplication2(
    1)
       
    End If
       
    Dim j As HttpJob
       j.Initialize(
    "j", Me)
       j.Download(Args(
    0))
       StartMessageLoop
    End Sub

    Sub JobDone(Job As HttpJob)
       
    If Job.Success Then
         
    Log(Job.GetString)
       
    Else
         
    Log(Job.ErrorMessage)
       
    End If
       Job.Release
       ExitApplication2(
    0'exit the application. Without this call the program will just hang.
    End Sub
    The jar file created is not an executable jar. You need to run it by calling the java program.
    The syntax is:
    Code:
    java -cp <jar file> <package>.main <args>
    You can create a batch file or script to run it.

    [​IMG]

    Note that instead of referencing jHttpUtils2, we are using its source code. jHttpUtils2 library depends on jFX (for the GetBitmap method which is removed here).
     

    Attached Files:

    Last edited: Dec 9, 2013
    hibrid0, B4JExplorer and mrred128 like this.
  2. mrred128

    mrred128 Active Member Licensed User

    Is there any provision for console input?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    You can use JavaObject to read the System input stream. You will need to run it in Release mode. The program will fail, however you will be able to start it from a cmd window:
    Code:
    Sub Process_Globals
       
    Private reader As TextReader
    End Sub

    Sub AppStart (Args() As String)
       
    Dim sys As JavaObject
       sys.InitializeStatic(
    "java.lang.System")
       reader.Initialize(sys.GetField(
    "in"))
       
    Log("Enter name: ")
       
    Log("You have entered: " & ReadLine)
       
    Log("Enter family name: ")
       
    Dim f As String = ReadLine
       
    Log("Family: " & f)
    End Sub

    Sub ReadLine As String
       
    Return reader.ReadLine
    End Sub
    [​IMG]
     
    koaunglay, giga and mrred128 like this.
  4. mrred128

    mrred128 Active Member Licensed User

    I guess for debugging purposes, you could set up a text file to read the input from. You would just have to comment out that code for the release.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You can use a Try / Catch block to decide whether you need to read the input from a file or from the console:
    Code:
    Try
     reader.Initialize(sys.GetField(
    "in"))
    Catch
     
    Log("IDE Mode")
     reader.Initialize(
    File.OpenInput(...))
    End Try
     
    lemonisdead and mrred128 like this.
  6. positrom2

    positrom2 Active Member Licensed User

    I tried the original program, getting the error:
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Have you created a non-UI app?
     
  8. positrom2

    positrom2 Active Member Licensed User

    I merely copied the example from post #3. What needs to be added?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    When you create a new project, you should choose non-UI app:
    [​IMG]
     
  10. positrom2

    positrom2 Active Member Licensed User

    Thank you, I missed to change for that since the IDE comes in UI-mode when starting the application...
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    I've added this image to the tutorial. It was not clear enough.
     
  12. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    How can we trap Non-UI application exit ?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  14. koaunglay

    koaunglay Member Licensed User

    Hi Erel! Can I use it in UI Application.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    There is no console in UI applications.
     
    Last edited: Jul 21, 2015
    koaunglay likes this.
  16. koaunglay

    koaunglay Member Licensed User

    Thanks Erel . I mean can I run .bat file.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Yes, you can start UI applications from a batch file. Please start a new thread if you need more information.
     
  18. tufanv

    tufanv Expert Licensed User

    can ui applications run on my linux vps server or does it have to be non-ui ?
     
  19. mrred128

    mrred128 Active Member Licensed User

    It has to have X11 (gui) installed, and running inside it. Even if you only use console i/o, it get's linked with guicode and will not run without it. You need to know at project start time how you need your app run. It's a bit of a pain, after the fact.
     
  20. mrred128

    mrred128 Active Member Licensed User

    A console based app doesn't care and will run in both.
     
Loading...