Execute commands in irb from java - 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.

Related

Impersonating an user in Linux through Java

I have a Java application that runs on a Linux server and performs series of actions (create directory, copies few files, checks if an application is running etc). I am using log4j2 for logging messages and it has been running fine.
I've a requirement to read an account and impersonate as that account (passwordless sudo switch user) and then perform series of above actions. Access to do a passwordless sudo has been taken.
I've already done this using a shell (first switch user and then execute the Java program) however I've raised this question to understand if this can be done within Java program ?
Execution: java -jar xyz.jar parm1
To evaluate, I changed the Java code to run a shell command as below (I am using processbuilder):
When I run the Java program, the logs appear only till the point of sudo shell command execution and no logs are shown post that step.
Is there a way I can retain all logs before and after switching user (impersonating) ? (I'm using log4j2 and writing to a log file which has write access to other users).
Please share if there is any known solution ?
[EDIT] Adding java code snippet used to trigger sudo
public boolean switchUser(String account) {
ShellCommand shellCommand = new ShellCommand();
List<String> cmd = new ArrayList<String>();
cmd.add("su");
//....I construct asu command for the passed account here. I have passwordless sudo su access and tested fine manually
//I've some logger messages here
shellCommand.runCommand(cmd); //Class with method that uses ProcessBuilder and executes shell command
}
[EDIT] I think when I trigger su from Java it opens a new shell while Java is waiting for it to complete. This is possibly the reason of no logs visible there after. So one solution will be to Wrap it in a shell, do a su with command to run the jar.

Executing shell script from 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.

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.

How to open a command prompt and input different commands from a java program

I need to open a command prompt & execute ij.bat, which will take me to another prompt, where I can give command to connect to DB & later different sql statements. I was able to execute ij.bat from the java program like
Process process = Runtime.getRuntime().exec("D:\\ij.bat");
But how to give the furthur set of commands through the java program?
You can use this exec
public Process exec(String command)
throws IOException
Also visit This Link
Instead of trying to run ij.bat, and feed it commands to run and parse the output, just have your program use the ij class, and its runScript method: http://db.apache.org/derby/docs/10.11/publishedapi/org/apache/derby/tools/ij.html#runScript-java.sql.Connection-java.io.InputStream-java.lang.String-java.io.OutputStream-java.lang.String-
Then you don't have to spawn a separate process, but can just keep everything in pure Java in your program itself.

Categories

Resources