B4J Question B4J Updeter

alirezahassan

Active Member
Licensed User
hi all,
i want to have Updater in my project. i wrote these codes but i have error.
First my app download zip file which has bin, conf, legal, lib And exe files. then my app extract this zip file to 'update' Folder. then Run These codes.
2021-08-31_00-33-57.png


2021-08-31_00-36-25.png


B4J code:
    File.WriteString(Codes.Get_FileDir,"Updater.bat",$"@ echo off       <-There is no space between @ and echo.
title Updater of Application
echo Please wait. Application is updating...
timeout /t 3
Rmdir /S /Q bin
Rmdir /S /Q conf
Rmdir /S /Q legal
Rmdir /S /Q lib
del /F /Q Panel.exe
xcopy /e update
Rmdir /S /Q update
start "" Panel.exe
del Updater.bat"$)
    Sleep(1000)
    fx.ShowExternalDocument(File.GetUri(Codes.Get_FileDir,"Updater.bat"))
    Sleep(10)
    ExitApplication

These codes first create a bat file. And open the file. Then close the application. It then deletes the previous application. And then moves the new application And opens.
but i have error
2021-08-31_00-39-49.png

Did not delete any files. And did not convey anything.
2021-08-31_00-40-58.png

The bat file is correct.
2021-08-31_00-42-25.png

Sometimes it works fine when I open the bat file myself.
and show me
Please wait. Application is updating...

Waiting for 0 seconds, press a key to continue ...
update\Panel.exe
update\bin\api-ms-win-core-console-l1-1-0.dll
update\bin\api-ms-win-core-datetime-l1-1-0.dll
update\bin\api-ms-win-core-debug-l1-1-0.dll
update\bin\api-ms-win-core-errorhandling-l1-1-0.dll
update\bin\api-ms-win-core-file-l1-1-0.dll
update\bin\api-ms-win-core-file-l1-2-0.dll
update\bin\api-ms-win-core-file-l2-1-0.dll
update\bin\api-ms-win-core-handle-l1-1-0.dll
update\bin\api-ms-win-core-heap-l1-1-0.dll
update\bin\api-ms-win-core-interlocked-l1-1-0.dll
update\bin\api-ms-win-core-libraryloader-l1-1-0.dll
update\bin\api-ms-win-core-localization-l1-2-0.dll
update\bin\api-ms-win-core-memory-l1-1-0.dll
update\bin\api-ms-win-core-namedpipe-l1-1-0.dll
update\bin\api-ms-win-core-processenvironment-l1-1-0.dll
update\bin\api-ms-win-core-processthreads-l1-1-0.dll
update\bin\api-ms-win-core-processthreads-l1-1-1.dll
update\bin\api-ms-win-core-profile-l1-1-0.dll
update\bin\api-ms-win-core-rtlsupport-l1-1-0.dll
update\bin\api-ms-win-core-string-l1-1-0.dll
update\bin\api-ms-win-core-synch-l1-1-0.dll
update\bin\api-ms-win-core-synch-l1-2-0.dll
update\bin\api-ms-win-core-sysinfo-l1-1-0.dll
update\bin\api-ms-win-core-timezone-l1-1-0.dll
update\bin\api-ms-win-core-util-l1-1-0.dll
update\bin\api-ms-win-crt-conio-l1-1-0.dll
update\bin\api-ms-win-crt-convert-l1-1-0.dll
update\bin\api-ms-win-crt-environment-l1-1-0.dll
update\bin\api-ms-win-crt-filesystem-l1-1-0.dll
update\bin\api-ms-win-crt-heap-l1-1-0.dll
update\bin\api-ms-win-crt-locale-l1-1-0.dll
update\bin\api-ms-win-crt-math-l1-1-0.dll
update\bin\api-ms-win-crt-multibyte-l1-1-0.dll
update\bin\api-ms-win-crt-private-l1-1-0.dll
update\bin\api-ms-win-crt-process-l1-1-0.dll
update\bin\api-ms-win-crt-runtime-l1-1-0.dll
update\bin\api-ms-win-crt-stdio-l1-1-0.dll
update\bin\api-ms-win-crt-string-l1-1-0.dll
update\bin\api-ms-win-crt-time-l1-1-0.dll
update\bin\api-ms-win-crt-utility-l1-1-0.dll
update\bin\awt.dll
update\bin\decora_sse.dll
update\bin\fontmanager.dll
update\bin\freetype.dll
update\bin\fxplugins.dll
update\bin\glass.dll
update\bin\glib-lite.dll
update\bin\gstreamer-lite.dll
update\bin\j2gss.dll
update\bin\java.dll
update\bin\java.exe
update\bin\javafx_font.dll
update\bin\javafx_iio.dll
update\bin\javajpeg.dll
update\bin\javaw.exe
update\bin\jawt.dll
update\bin\jfxmedia.dll
update\bin\jimage.dll
update\bin\jli.dll
update\bin\jrunscript.exe
update\bin\jsound.dll
update\bin\keytool.exe
update\bin\kinit.exe
update\bin\klist.exe
update\bin\ktab.exe
update\bin\lcms.dll
update\bin\mlib_image.dll
update\bin\msvcp140.dll
update\bin\net.dll
update\bin\nio.dll
update\bin\Panel.exe
update\bin\prefs.dll
update\bin\prism_common.dll
update\bin\prism_d3d.dll
update\bin\prism_sw.dll
update\bin\release_java_modules.txt
update\bin\splashscreen.dll
update\bin\sunec.dll
update\bin\ucrtbase.dll
update\bin\vcruntime140.dll
update\bin\verify.dll
update\bin\w2k_lsa_auth.dll
update\bin\zip.dll
update\bin\server\jvm.dll
update\conf\logging.properties
update\conf\net.properties
update\conf\sound.properties
update\conf\security\java.policy
update\conf\security\java.security
update\conf\security\policy\README.txt
update\conf\security\policy\limited\default_local.policy
update\conf\security\policy\limited\default_US_export.policy
update\conf\security\policy\limited\exempt_local.policy
update\conf\security\policy\unlimited\default_local.policy
update\conf\security\policy\unlimited\default_US_export.policy
update\legal\java.base\ADDITIONAL_LICENSE_INFO
update\legal\java.base\aes.md
update\legal\java.base\asm.md
update\legal\java.base\ASSEMBLY_EXCEPTION
update\legal\java.base\c-libutl.md
update\legal\java.base\cldr.md
update\legal\java.base\icu.md
update\legal\java.base\LICENSE
update\legal\java.base\public_suffix.md
update\legal\java.base\unicode.md
update\legal\java.base\zlib.md
update\legal\java.datatransfer\ADDITIONAL_LICENSE_INFO
update\legal\java.datatransfer\ASSEMBLY_EXCEPTION
update\legal\java.datatransfer\LICENSE
update\legal\java.desktop\ADDITIONAL_LICENSE_INFO
update\legal\java.desktop\ASSEMBLY_EXCEPTION
update\legal\java.desktop\colorimaging.md
update\legal\java.desktop\freetype.md
update\legal\java.desktop\giflib.md
update\legal\java.desktop\harfbuzz.md
update\legal\java.desktop\jpeg.md
update\legal\java.desktop\lcms.md
update\legal\java.desktop\libpng.md
update\legal\java.desktop\LICENSE
update\legal\java.desktop\mesa3d.md
update\legal\java.desktop\opengl.md
update\legal\java.logging\ADDITIONAL_LICENSE_INFO
update\legal\java.logging\ASSEMBLY_EXCEPTION
update\legal\java.logging\LICENSE
update\legal\java.naming\ADDITIONAL_LICENSE_INFO
update\legal\java.naming\ASSEMBLY_EXCEPTION
update\legal\java.naming\LICENSE
update\legal\java.prefs\ADDITIONAL_LICENSE_INFO
update\legal\java.prefs\ASSEMBLY_EXCEPTION
update\legal\java.prefs\LICENSE
update\legal\java.scripting\ADDITIONAL_LICENSE_INFO
update\legal\java.scripting\ASSEMBLY_EXCEPTION
update\legal\java.scripting\LICENSE
update\legal\java.security.jgss\ADDITIONAL_LICENSE_INFO
update\legal\java.security.jgss\ASSEMBLY_EXCEPTION
update\legal\java.security.jgss\LICENSE
update\legal\java.security.sasl\ADDITIONAL_LICENSE_INFO
update\legal\java.security.sasl\ASSEMBLY_EXCEPTION
update\legal\java.security.sasl\LICENSE
update\legal\java.xml\ADDITIONAL_LICENSE_INFO
update\legal\java.xml\ASSEMBLY_EXCEPTION
update\legal\java.xml\bcel.md
update\legal\java.xml\dom.md
update\legal\java.xml\jcup.md
update\legal\java.xml\LICENSE
update\legal\java.xml\xalan.md
update\legal\java.xml\xerces.md
update\legal\java.xml.crypto\ADDITIONAL_LICENSE_INFO
update\legal\java.xml.crypto\ASSEMBLY_EXCEPTION
update\legal\java.xml.crypto\LICENSE
update\legal\java.xml.crypto\santuario.md
update\legal\javafx.base\ADDITIONAL_LICENSE_INFO
update\legal\javafx.base\ASSEMBLY_EXCEPTION
update\legal\javafx.base\LICENSE
update\legal\javafx.controls\ADDITIONAL_LICENSE_INFO
update\legal\javafx.controls\ASSEMBLY_EXCEPTION
update\legal\javafx.controls\LICENSE
update\legal\javafx.fxml\ADDITIONAL_LICENSE_INFO
update\legal\javafx.fxml\ASSEMBLY_EXCEPTION
update\legal\javafx.fxml\LICENSE
update\legal\javafx.graphics\ADDITIONAL_LICENSE_INFO
update\legal\javafx.graphics\ASSEMBLY_EXCEPTION
update\legal\javafx.graphics\jpeg_v7.md
update\legal\javafx.graphics\LICENSE
update\legal\javafx.media\ADDITIONAL_LICENSE_INFO
update\legal\javafx.media\applecoreaudio.md
update\legal\javafx.media\ASSEMBLY_EXCEPTION
update\legal\javafx.media\glib.md
update\legal\javafx.media\gstreamer.md
update\legal\javafx.media\libffi.md
update\legal\javafx.media\LICENSE
update\legal\javafx.swing\ADDITIONAL_LICENSE_INFO
update\legal\javafx.swing\ASSEMBLY_EXCEPTION
update\legal\javafx.swing\LICENSE
update\legal\jdk.charsets\ADDITIONAL_LICENSE_INFO
update\legal\jdk.charsets\ASSEMBLY_EXCEPTION
update\legal\jdk.charsets\LICENSE
update\legal\jdk.crypto.ec\ADDITIONAL_LICENSE_INFO
update\legal\jdk.crypto.ec\ASSEMBLY_EXCEPTION
update\legal\jdk.crypto.ec\ecc.md
update\legal\jdk.crypto.ec\LICENSE
update\legal\jdk.jsobject\ADDITIONAL_LICENSE_INFO
update\legal\jdk.jsobject\ASSEMBLY_EXCEPTION
update\legal\jdk.jsobject\LICENSE
update\legal\jdk.unsupported\ADDITIONAL_LICENSE_INFO
update\legal\jdk.unsupported\ASSEMBLY_EXCEPTION
update\legal\jdk.unsupported\LICENSE
update\legal\jdk.unsupported.desktop\ADDITIONAL_LICENSE_INFO
update\legal\jdk.unsupported.desktop\ASSEMBLY_EXCEPTION
update\legal\jdk.unsupported.desktop\LICENSE
update\legal\jdk.xml.dom\ADDITIONAL_LICENSE_INFO
update\legal\jdk.xml.dom\ASSEMBLY_EXCEPTION
update\legal\jdk.xml.dom\LICENSE
update\lib\classlist
update\lib\fontconfig.bfc
update\lib\fontconfig.properties.src
update\lib\javafx-swt.jar
update\lib\javafx.properties
update\lib\jawt.lib
update\lib\jrt-fs.jar
update\lib\jvm.cfg
update\lib\jvm.lib
update\lib\modules
update\lib\psfont.properties.ja
update\lib\psfontj2d.properties
update\lib\tzdb.dat
update\lib\tzmappings
update\lib\security\blacklisted.certs
update\lib\security\cacerts
update\lib\security\default.policy
update\lib\security\public_suffix_list.dat
update\lib\server\Xusage.txt
213 File(s) copied

what should i do?
 

Attachments

  • 2021-08-31_00-36-25.png
    2021-08-31_00-36-25.png
    6.8 KB · Views: 213
Last edited:

roerGarcia

Active Member
Licensed User
Longtime User
does the script run in the correct folder?
It is a research question.
try to secure the script directory before running
Or the synchronization between deleting and copying the new package fails
 
Upvote 0

alirezahassan

Active Member
Licensed User
You should use jShell and run cmd.exe /c to start the batch file.
Thanks for the reply
Are you sure this code works before the program exits?
I checked the library but did not notice.
Can you help me?
@Erel
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thanks for the reply
Are you sure this code works before the program exits?
I checked the library but did not notice.
Can you help me?
@Erel

Hi @alirezahassan. Did you make it work with the code? Does it require elevated rights like being administrator?
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
One thing to remember is that if you "Run As Administrator" that the working directory will be C:\Windows\System32 (even for batch files)

1631218766793.png


So if that is the case you need to fully qualify your paths.

Eg this batch file uninstalls a windows service. Even though ServiceName.exe is in the same diretcory as the batch file you need to grab the path of the batch file (in homedir variable) and use that to fuilly qualify the path.

B4X:
@echo off
setlocal

net stop "Service Name"

SET homedir=%~dp0
IF %homedir:~-1%==\ SET homedir=%homedir:~0,-1%

%homedir%\ServiceName.exe uninstall
 
Upvote 0

alirezahassan

Active Member
Licensed User
hi @hatzisn and @tchart
i changed my code like these.

B4X:
@echo off
title Updater of Application
setlocal
net stop "Service Name"
SET homedir=%~dp0
IF %homedir:~-1%==\ SET homedir=%homedir:~0,-1%
%homedir%\ServiceName.exe uninstall
echo Please wait. Application is updating...
timeout /t 3
Rmdir /S /Q bin
Rmdir /S /Q conf
Rmdir /S /Q legal
Rmdir /S /Q lib
del /F /Q Panel.exe
xcopy /e update
Rmdir /S /Q update
start "" Panel.exe
del Updater.bat

But it gives an error when run with my application. (It does not give an error when I open the batch file myself.)

2021-09-14_12-46-14.png
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Change the line "net..." to "runas /noprofile /user:Administrator net..."

Change the Rmdir lines when creating in B4J to (f.e.) :

"Rmdir /S /Q " & File.Combine(File.DirApp, "..\bin")

and "Del " & File.Combine(File.DirApp, "..\Panel.exe")

Change the xcopy line in the .bat when you are creating the bat file to:

"xcopy /e " & File.Combine(File.DirData , "update") & " " & File.Combine(File.DirApp, "..")

Save the ".bat" file in File.DirData and save there also the zip which you will unzip.



I believe also you will have to do the following in code :

B4X:
    Dim js As Shell
    Dim sRet As String
    Dim jres As ShellSyncResult
    js.Initialize("js", "runas", Array As String ("/noprofile", "/user:Administrator", "cmd", "/c", File.Combine(File.DirData, "Update/Updater.bat")))
    jres = js.RunSynchronous(5000)  '<- maybe you will have to change this to a greater value for waiting
    sRet = jres.StdErr
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I have changed several things in my previous message so if @alirezahassan you have already seen it before I post this message you'd better see it again.
 
Upvote 0
Top