Frame Buffer Image Viewer (FBI) Failing when Executed as Process - Java - java

I'm trying to use the Linux FBI utility to write a few .jpg images to the framebuffer on a Raspberry Pi 3 from Java.
From the command line everything works as expected. My java application runs fine until I attempt to run the following line of code (no Java errors by the way):
Process p = Runtime.getRuntime().exec("sudo fbi -T 1 -d /dev/fb0 -a -noverbose -t 1 -cachemem 0 /home/pi/Desktop/*.jpg");
The black loading screen for FBI does display when this process is executed so I know that it's executing properly, but FBI responds with an error stating /home/pi/Desktop/*.jpg Loading Failed. I've seen this error before but only when I'm referencing a folder or file that doesn't exist. The images that I am trying to display are on the Desktop. I can run the same command that is in the call to exec from any location in the CLI and it works. I'm not real sure why it's not working from my Java application.
Thanks in advance

Simple: the "*" wildcard (or any other wildcard) is a feature of the underlying shell. Therefore it works when you use it manually on the command line.
But when using it via the process builder, there is no shell. Thus there is no component that turns the asterisk into a list of file names. Thus that string is passed as file name, and of course, there is no such file!
You either have to write Java code that expands the wildcard in code (to then pass a list of file names directly) or you have to actually start a shell explicitly (getting that right, with all the commands might be quite tricky).

Related

How to make an executable File

Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;

How to run jlink-generated Java runtime image without CMD window?

I've created Java runtime image for a simple OpenJFX application. In order to run this app, jlink auto-generated two lauch scripts under %image_path%/bin directory. This how it looks like (the one for Windows):
#echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
"%DIR%\java" %JLINK_VM_OPTIONS% -m app/com.package.Launcher %*
Obviously, when I run this batch file it opens new shell window, which is not what I want to. I've tried all common approaches: use javaw instead of java, run script via start command etc. Nothing works.
Is it possible to avoid shell window or somehow create native launcher?
Ok, I've figured out it's not posiible to eliminite shell window completely. In the best scenario it's just flickers for ~1sec. This is how it can be achieved:
#echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
start "" "%DIR%\javaw" %JLINK_VM_OPTIONS% -m app/com.package.Launcher %* && exit 0
There is a feature request about native laucher implementation but it's not discussed actively.
Nonetheless I've solved the problem. There is "Batch to EXE Converter" tool. It can generate executable (basically the same batch file) which can run your app silently.
This looks to be possible using vbscript. If you put the following script into a .vbs file next to the launcher.bat file (or whatever the name of the batch file is):
CreateObject("Wscript.Shell").Run CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\launcher.bat " & WScript.Arguments(0) & " > " & CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\launch-log.log", 0, False
This runs the batch file in the same directory, and also redirects stdout to a log file.
What you´d like to achieve is very well possible. It is actually even quite easy and I use this every day. There already is an early access build of jpackage available here: http://jdk.java.net/jpackage/ Creating executables works already
nicely (I use it on Mac and Windows). Only creating installers is still a bit problematic.
It's very easy to run a bat file without showing the cmd window.
you just need to create VBS file to run bat file with the following cmd
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & ".\bin\launcher.bat" & Chr(34), 0
Set WshShell = Nothing
Save cmd in the file out of the bin folder with any name like
Launcher.vbs.

Launch powershellscript in java application

Is it possible to run an entire, large, powershell script in a java application without calling it externally by launching a powershell process with the -file parameter? (passing it via the encodedcommand parameter won't work either because of the commandline lenght limitation).
I.e. is there a java library that enables you to paste your powershellscript inside your java app and run it?
I currently embed the powershellscript inside the java application and write it to disk, but I'm looking for a fileless approach.
Since you want to pass a large program, piping from the Java program to the PS script is more suitable than using the command line or environment variables. Since I don't know too much Java and don't have it installed, I'll simulate it from the PS command line in the snippet below. Your Java program would read the lines of the program from a list or some other suitable data structure (whereas this example reads it from the file system). I did see hits on google for piping from Java to an externally executed command.
cat my.ps1|
powershell -command {$scr="";foreach ($line in $input){$scr+=$line+"`n"} echo bxb $s cxc}
After your PS stub has received the program rather than echo it, it would execute it: iex $scr.
If you need to pass parameters from Java to PS you could either pipe them along with the program (for example as lines of code that set global variables) or they could be global variables set in stub code. Other, more complex, variations are possible.

How to run matlab on the background?

I'm currently using an external editor of Matlab .m files, with a custom build system that calls Matlab from the command line to run the Matlab script (with the -nosplash and -nodesktop). However this creates two problems:
1) Matlab closes right after running the script: any windows or plots I call in the script are closed right after running the script, which obviously happens in a matter of seconds.
2) There is a slight delay every time I run the script because Matlab is effectively being started from scratch.
So I was wondering if would be possible to have Matlab running in the background, and just running the scripts whenever I want?
I'm running Linux 64bits, Matlab 2013a, and Sublime Text 3.
EDIT: I've testing the setup with a basic script:
a=5;
figure
plot(a);
EDIT2: I'm calling Matlab through a Sublime Text build system that runs:
matlab -nosplash -nodesktop <[script].m
There is no way to have Matlab running in the background and "just running the scripts whenever you want" without having an interactive session open somewhere.
Suppose that your system has a custom wrapper matlab-wrapper that is used to submit scripts in the background. You would call your script like this:
$ matlab-wrapper myscript.m
Likely, matlab-wrapper is doing something like this:
#!/bin/bash
/apps/matlab14a/bin/matlab -nodesktop -nosplash -r run\ "$1",exit
Or even more, submitting the above script to a scheduler via qsub or some other command.
The key would be modify the wrapper script to find the part where the Matlab binary is actually invoked. If your system allows, you could copy the wrapper script and modify it. (Either by simply removing the -r run\ "$1" text or something more complicated.) Then, you should be able to launch an interactive version of Matlab per the custom configuration on your system, and call your scripts from the Matlab command window.

Redirecting INTO a Java program?

In a project, I'm trying to set up an automated build system for Apache Karaf (there are several commands I need to run in Karaf to set up a working environment on a fresh install). Karaf contains a batch/script file that sets several parameters, and then calls the actual Java program. Essentially, I'd like to be able to do something like:
java MyProgramClass.class < commandTextFile.txt
But when I try this it doesn't do anything. My goal is to simply copy the karaf.bat file, modify it slightly (as below) to make a "karaf-install.bat" that I can just run. The part I've modified of karaf.bat is below, and all I've done is add < "C:\commandFile.txt at the end (the following is all on one line, broken for readability):
"%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%"
-Djava.endorsed.dirs="%JAVA_HOME%\jre\lib\endorsed;%JAVA_HOME%\lib\endorsed;%KARAF_HOME%\lib\endorsed"
-Djava.ext.dirs="%JAVA_HOME%\jre\lib\ext;%JAVA_HOME%\lib\ext;%KARAF_HOME%\lib\ext"
-Dkaraf.instances="%KARAF_HOME%\instances" -Dkaraf.home="%KARAF_HOME%"
-Dkaraf.base="%KARAF_BASE%" -Dkaraf.data="%KARAF_DATA%"
-Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties"
%KARAF_OPTS% %MAIN% %ARGS% < "C:\commandFile.txt"
However, Karaf shows nothing. It just runs as if I executed it as normal; my commands are not executed. Is there a way to redirect INTO a java program from the console? Am I doing it way wrong?
For what it's worth, this will eventually be done on both Windows and OS X, but I'm focusing on Windows at the moment.
Update: turns out that this seems to work for me on OS X (Karaf struggles (by saying "Command not found: "), but I think it's because it's getting the commands before it's initialized everything), but Windows is still doesn't even get the commands. I'll poke around more.
When piping INTO, you can read it from System.in.
Consider it a Reader, not an InputStream.
I'm just going to write this issue off as Karaf weirdness, seeing as it works on OS X. I was able to work around it by using the client program that comes with Karaf by doing (on OS X in a .sh file):
"$KARAF/bin/client" "karaf_command_here"
or (on Windows in a .bat file)
call "%KARAF%\bin\client.bat" "karaf_command_here"
And instead of having a list of commands to pipe into Karaf, I just made the list of commands a shell/batch script file that would call Karaf's client for each command. Not as pretty as I'd have liked it, but it got the job done.
(Note you need to start Karaf before using the client with start (and close it with stop)).

Categories