B4J Question Execute Shell .exe from within Files app?

MathiasM

Active Member
Licensed User
Hello

I try execute an exe file with the shell. This is the code I'm using:

B4X:
sh.Initialize("split", File.Combine(File.DirAssets, "cpdf.exe"), Array As String("-split", txtPDFPath.Text, "-o", File.Combine(File.DirTemp & SplittedPDFFolder, "page%%%%%.pdf")))
    sh.Run(10000)
    Wait For split_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(StdOut)
    Log(StdErr)

So I'm trying to launch cpdf.exe in the Assetfolder. But I get this error:
org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "AssetsDir\cpdf.exe" (in directory "."): CreateProcess error=2, The system cannot find the file.)

The odd thing is, the file is certainly in the folder:
iK7slkv.png


I tried another way, specifying the workingdirectory:

B4X:
sh.Initialize("split", "cpdf.exe", Array As String("-split", txtPDFPath.Text, "-o", File.Combine(File.DirTemp & SplittedPDFFolder, "page%%%%%.pdf")))
    sh.WorkingDirectory = File.DirAssets
    sh.Run(10000)

However, that also throws an error (but another)
Waiting for debugger to connect...
Program started.
Error occurred on line: 75 (Main)
java.io.IOException: AssetsDir doesn't exist.
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:187)
at anywheresoftware.b4j.objects.Shell.run(Shell.java:209)
at anywheresoftware.b4j.objects.Shell.Run(Shell.java:155)
at b4j.example.main$ResumableSub_SplitPDF.resume(main.java:1207)
at b4j.example.main._splitpdf(main.java:1162)
at b4j.example.main$ResumableSub_txtPDFPath_TextChanged.resume(main.java:1338)
at b4j.example.main._txtpdfpath_textchanged(main.java:1286)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

Does anyone know what's wrong? Thanks for any insights.
 

MathiasM

Active Member
Licensed User
Hi Enrique

Thanks, your solution works. For future reference, this is my working code:
B4X:
File.Copy(File.DirAssets, "cpdf.exe", File.DirApp, "cpdf.exe")
Dim sh As Shell
sh.Initialize("split", "cpdf.exe", Array As String("-split", txtPDFPath.Text, "-o", File.Combine(File.DirTemp & SplittedPDFFolder, "page%%%%%.pdf")))
sh.WorkingDirectory = File.DirApp
sh.Run(10000)
 
Last edited:
Upvote 0
Top