I am trying to open a vi editor using Java code in an linux env (the java code is executed via shell script). The editor should open in foreground & become active terminal, while the java is should be running at Background.
I tried using both commands using :-
String []command = {"xterm", "vi", "/home/user/test.txt"};
Process pr = Runtime.getRuntime().exec(command);
Process p = new ProcessBuilder("vi", "/home/user/test35.txt").start();
In one of the above code, if check the ps -ef | grep vi, I am able to the process, but its running at background. I want to run it in foreground as an active terminal as user for his type the text into the editor. While the java will be running at the background.
Any suggestion or snippet's.
I have referred this Open VIM with Java application , but still in vain.
If you want to create a new xterm and execute a command in that terminal, you need to pass the command with -e. Try this:
ProcessBuilder pb = new ProcessBuilder("xterm", "-e", "vi", "/home/user/test.txt");
The debug steps I did was I tried opening a terminal via command & use the same command in the Java code. Issue observed that I need to set DISPLAY=:0. if I am running via root user , for other user export DISPLAY was not needed
String []command = {"/usr/bin/xterm","-e", "vi", "/home/hscpe/test.txt"};
Process pr = Runtime.getRuntime().exec(command);
Since I am running the java code via shell script I will be adding export in the shell script i.e export DISPLAY=:0. Now will try to make the editor as editable (Will stimulate by pressing I , i.e Insert by java robot).
I referred here & here
Related
OS: Windows 7 Enterprise, SP1
Adobe FMLE 3.2
I was always executing FMLECmd.exe from Java-code without any problems. But suddenly smth happened and the stream couldn't start anymore.
Setting the compatibility mode to Windows XP SP3 solved the problem of execution.
But the new one appeared: launching the stream in compatibility mode should be performed as Administrator. I switched off the UAC popup and solved the problem of programmatical stream-starting.
But then the new problem appeared: when I want to kill the FMLECmd.exe process programmatically (to stop the stream) I get the message that 'Access is denied'. I guess the reason is that I started stream as Administrator, but process-killing is held as a usual user.
So, the question: Are there any ways to make FMLE work without compatibility mode? Or are there any ways to kill that process without being administrator from Java?
Didn't find how to run it without compatibility mode but could kill the process:
Create a batch file which contains
taskkill /F /IM FMLECmd.exe
Create a shortcut of this batch file
Go to the properties of shortcut.
Choose Shortcut tab
Click Advanced btn
Tick Run as administrator option
Start the shortcut using this java code:
String command = "start "+ pathToShortcut;
String[] cmd = { "cmd.exe", "/c", command };
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.start();
That worked for me
I am aware that you can pass commands to the Terminal and it returns your result in Java. The problem is for undisclosed reasons I need a Terminal window to open and to have the command in it. This code works to open an empty Terminal window (on mac):
p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal");
And I have seen someone say on a different overflow question that you can do this:
p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal [Put Command to pass through here]");
But it didn't seem to work when I tried to pass the command through to the window.
Any help or suggestions?
You can achieve this by using AppleScript.
Here you can find example how call it from Java:
AppleScript from Java on Mac OS X 10.6?
And script you need will look like this:
tell application "Terminal"
set currentTab to do script ("cd ~/Downloads/")
set currentTab to do script ("ll")
end tell
For examples, this is my bash script
osascript -e 'tell app "Terminal"
do script "./process1"
end tell'
osascript -e 'tell app "Terminal"
do script "./process2"
end tell'
Basically, it will open two different terminal windows (on mac) and execute defined commands. I try to do this in java by
process1 = Runtime.getRuntime().exec(new String[]{"process1"});
process2 = Runtime.getRuntime().exec(new String[]{"process2"});
The problem is it seems that there is only one terminal is opened (and not visible - it runs in background) and then two command process1 and process2 are executed. But because the process 1 will keep that terminal busy thus process2 cannot run. That's why I want to open different terminal to execute those commands.
Create a thread for each one of them. and give a time space "sleep(for some time thread 1 or 2)" and this will run both depending on you operating system.
I need my Java console application to do the following:
Open VIM
< the user input multiple lines of words and :wq >
My application should then be able to get the vim input and print it in the terminal.
So far I'm stuck at 1.. It seems impossible to get Java to open VIM!
Here is some non functional code I have been fiddling with:
call system text editor
http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java
Advice much appreciated
EDIT
Ok so the following will open VIM but backgrounded. Any way around this?
String[] command = {"/usr/bin/vim", "test.txt"};
Process vimProcess = Runtime.getRuntime().exec(command);
vimProcess.waitFor();
run vim from java (notice that this will work on gnome, if you use KDE use "konsole"):
Process pr = Runtime.getRuntime().exec("gnome-terminal -e 'vim /tmp/tmpfile'");
wait the user will finish and exit:
int exitVal = pr.waitFor(); //check for the right exitVal
read the input and write to the terminal:
System.out.println(FileUtils.raedFileToString(new File("/tmp/tmpfile")));
What's the best way to restart a java app in ubuntu? I know u can run commands like this one from the terminal, but it doesnt seem to be working...
String restartArgs = "java -jar \'/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar\' --Terminal=true";
Process restart = Runtime.getRuntime().exec(restartArgs);
System.exit(1);
You are killing the parent process with System.exit(1), so its child process is destroyed as well.
To restart you would typically provide a shell script wrapper to launch the actual Java app.
#!/bin/sh
restartCode="1"; # predefined restart signal code
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true; # run java program
if [ $? -eq restartCode ] # if exit code is equal to predefined restart signal code
then
$0; # restart script
fi
exit $?;
Note the above code is a rough, crude outline. Typical wrappers are far more complex to deal with commandline arguments passed to the startup script itself etc. etc. Plus, my sh-skills are not infallible.
try providing full path for JAVA_HOME (e.g /usr/lib/jvm/java-6-sun/bin/java instead of java). The exec does not have Shell enironment variables.
also use
restart.waitFor(); //wait until process finishes
to make sure Java does not exit before the process finishes.
If you do want to run in shell (and use shell specific stuffs like pipe and ls) do this:
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("java -jar /home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar");
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
int result = commandExecutor.executeCommand();
commandExecutor.waitFor(); //wait until process finishes