Executing shell script from Java - java

Able to call and execute the shell script when executed in an individual Java Program. But there is no output when called from a Floodlight controller program
The Floodlight controller is executed using java -jar target/floodlight.jar. The command to execute the shell script is provided in one of the source files. When ever the condition matches and code gets executed, the terminal screen appears for a second and vanishes. But this is not the case when I execute the same shell script with Java in an individual program.
Process proc = Runtime.getRuntime().exec(new String[]{"path to shell script", arg1});
Can anybody please comment on this ?

Executing with sudo or in root mode is the culprit.
Ran in user mode, worked perfectly.

Related

Send "sigterm" from Java to Bash script

I am starting a Java code from Bash script called start.sh. The Bash script fires up the Java code and then the Java code runs. At the end of the Java program, I want to send a signal back to the Bash script to terminate. Keep in mind that the Bash script runs with PID = 1. And I have to kill the PID 1 process.
I have the bash script set up such that it runs in an infinite loop and waits for a termination signal:
#!/bin/bash
# Run the java code here..
# Listen for an exit command.
trap 'exit 0' SIGTERM
while true; do :; done
I am using Docker instances and the signal is sigterm. I am following this tutorial: https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/ which explains how to send a sigterm from command line. I want to automate this process and close/kill the docker instance from inside as soon as the Java program ends.
How do I send the signal back to the bash script that started the Java code in the first place?
Should I follow
this method to send a signal as arguments to the bash script? Or will it start another bash script with a different PID (not 1).
Help needed!
Write 'set -e' in second line in bash script.
Dont use trap and while. Replace it by
'exec your_java_code_run'.
By this way docker get SIGTERM after java code run end.
Exemple:
#!/usr/bin/env bash
set -e
someOtherCodeIfNeed
exec your_java_code_run

Open gnome-terminal in background from Java

I'm trying to open gnome-terminal to execute a command from Java by the following code:
Runtime.getRuntime().exec(new String[]{"gnome-terminal", "-e", "command"});
But this code will show terminal for user, how I can open gnome-terminal in background?
You don't need to open a terminal visually to execute a command. Shell interpreter is a program which you can invoke without the terminal window. What terminal window does is it just sends what you type on the terminal, interacts with the underlying shell interpreter and gives you the results. When you need to run a OS specific command you would want to directly call the shell interpreter you want to with(i.e bash, csh, ksh) and get the result from within the Java program.
Working with commands and reading output from them is not that easy sometimes. So I would suggest you to have a look at Apache Exec which will ease reading data from externally invoked program.

Run shell script from Java to send data via USB

Exactly as specified in the title, I'm trying to send data to my ODROID-Show external screen via USB. I'm running a shell script that sends such data. The problem is I can simply run the command through Terminal and it runs successfully and data is sent to my little screen through USB port. When I try to run the same command via Java, Nothing happens.
Process proc = Runtime.getRuntime().exec("/bin/bash -c /home/ahmed/ODROID-SHOW-master/example/linux/images.sh /");
The specified command should have root privileges to run. That, I've switched to root then ran the code and nothing happened. Any thoughts how to solve such problem?
Edit:
IF you can show a code that executes given command prefixed by sudo this will absolutely work.
I was able to run the program as root. but, corrupted data are sent to ODROIDscreen rather than valid images. while it transfers successfully when ran through Terminal, Any thoughts why that happens?
I would check if the script executed by the bash interpreter requires certain environment variables set before execution.
I'd add a debug line in the executed shell script to dump the environment like "env > my_dump_env.txt" then run the script both from command line as well as from Java and do a diff see what is missing or is different.

Execute commands in irb from java

I want to start irb (Interactive Ruby Shell) in terminal using java and then execute different commands on that already opened irb shell when particular event occurs.
I have tried Runtime.exec() method but it executes commands on terminal or command prompt only and is unable to execute commands in irb shell. Also every time it executes commands on new instance of terminal instead of same instance. I want to execute multiple commands on same terminal instance instead of newly created instance of terminal.
Please suggest.
Thanks in advance.

apktool command in shell script does not execute from Java program

I wish to execute a simple bash Script from Java. This script is as follows:
cp /home/ashish/Downloads/apktool/apktool.jar /home/ashish/workspace/MyFirstApp/bin/apktool.jar
java -jar apktool.jar d -f MyFirstApp.apk
echo "Hello World"
The problem is only the cp command is executed and the last echo is executed. The second command doesn't execute. However, if I execute the second command from the command line, it runs well (the apk folder is created).
How can I make Java program execute the apktool command from the shell script ?
Thanks.
Well, in my experience, I would say that Java is not very fond of being ran by means of script.
I did a quick google search. Try the ProcessBuilder, it looks perfect for this situation, in my opinion.
I hope this helps you! :)

Categories

Resources