B4J Code Snippet [B4J] Restart App

hello everyone,
Here is a slightly improved code that allows you to restart the application at any time:

code source by @byz:

Main Form::
#Region Project Attributes
    #MainFormWidth:  600
    #MainFormHeight: 880
    #PackagerProperty: ExeName = Your.exe
'    #IgnoreWarnings:
#End Region

B4XMainPage::
Private Sub Button1_Click
    RestartSelf("Your.exe")
End Sub

Private Sub RestartSelf(exeName As String)
    Try
'       shl.Initialize("shl", "java.exe", Array As String("-jar", exeName))  'Your.jar
        shl.Initialize("shl", "cmd.exe", Array As String("/c", exeName))     'Your.exe
        shl.WorkingDirectory = File.DirApp
        shl.Run(-1)
    Catch
        Log(LastException.Message)
    End Try
    Sleep(0)
    ExitApplication
End Sub
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Hi @T201016,
May I know this snippet is improved from which code?

I also have seen a similar post here:
 

T201016

Active Member
Licensed User
Longtime User
Hi @T201016,
May I know this snippet is improved from which code?

I also have seen a similar post here:
Hello,
Yes, it's the same code that I slightly modified (see line 8 in B4XMainPage). Example @byz as it stands didn't work for me.
In the example, I provide the application name without the full path, but I set the application directory (i.e. shl.WorkingDirectory = File.DirApp)
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Hello,
Yes, it's the same code that I slightly modified (see line 8 in B4XMainPage). Example @byz as it stands didn't work for me.
I see.
I tested byz's code. It worked for me most of the time but does fail sometimes.
I haven't tested your code.

I think you missed the declaration for the shell.
B4X:
Private shl As Shell
 

T201016

Active Member
Licensed User
Longtime User
I think you missed the declaration for the shell.
No, I have it declared globally in the project. As for it not working sometimes, it's a shell issue
PS. I checked my modified example and it works for both .exe and .jar
 
Last edited:

Magma

Expert
Licensed User
Longtime User
I think that.. to be sure for working....
Need to have a time sleep ~10sec .. that no db or file will be opened...when app starts...

All have to do how fast disks have.. how will work exitapplication too.. if opened db, or files... need fast close them.
 

aeric

Expert
Licensed User
Longtime User
In my case, this code snippet doesn't work for me while byz's code works for me.
I think this is because the shell which will be executed from the "running process" exe is inside \bin directory must call the "start-up" exe which is located 1 level up from the \bin directory as you can see in run_debug.bat
 
Top