B4J Question Is my program running

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

I wish to check whether my background B4J app is running when I start it.

I have thought of using a check file of a map with the time to handle crashes. I also tried jps -l, but couldn't get it to run as it doesn't seem to be on a java path and when I switched directories it complained about no having a jvm.cfg file. The file idea with time is OK, but when debugging with very short time between runs and plenty of crashes it is a pain.


I saw this code in stack overflow: To check whether an application is running or not using java?

To check whether an application is running or not using java?:
boolean result = false;
String processName = "com.myapp.MyApp";

boolean running = false;
HostIdentifier hostIdentifier = new HostIdentifier("local://localhost");

MonitoredHost monitoredHost;
monitoredHost = MonitoredHost.getMonitoredHost(hostIdentifier);

Set activeVms = monitoredHost.activeVms();
for (Object activeVmId : activeVms) {
    VmIdentifier vmIdentifier = new VmIdentifier("//" + String.valueOf(activeVmId) + "?mode=r");
        MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmIdentifier);
    if (monitoredVm != null) {
        String mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
        if (mainClass.toLowerCase().equals(processName.toLowerCase())) {
            running = true;
            break;
        }
    }
}

System.out.print(running);
;

I think it makes a list of java apps running on the local jvm. Trouble is I don't know how to turn it into a B4x function/class.

I use VisualVM to keep track of what programs are running etc when debugging, This is a very handy app and can be downloaded for free at:

VisualVM Download

Viz:

Snag_35cb1a5.png



I am guessing this app uses a program like the one in stack exchange to produce the tree list.

Would it be possible to use the code in B4x in a suitable basic wrapper to produce a list of apps? I don't have a clue how to do this.

Best regards
Rob
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    test
    StartMessageLoop
End Sub

Private Sub test
    Wait For (CheckWhetherJavaAppIsRunning("aa.bb")) Complete (Running As Boolean)
    Log("running: " & Running)
End Sub

Private Sub CheckWhetherJavaAppIsRunning (ProgramName As String) As ResumableSub
    Dim shl As Shell
    shl.Initialize("shl", File.Combine(GetSystemProperty("java.home", ""), "bin\jps.exe"), Array("-l"))
    shl.Run(-1)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(StdOut)
    Return StdOut.Contains(ProgramName)
End Sub
 
Upvote 0
Top