I've been playing around with exec and although it opens a terminal with the user being me, it doesn't seem to be able to execute commands I give it =/ code is as follows:
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("xterm -e \"source /home/USER/.bashrc; ~/./myscript.sh\"")
}
catch(Throwable t)
t.printStackTrace();
In the xterm console I get:
xterm: Can't execvp ": No such file or directory
user#user:$
Which is then a terminal waiting to be used, and will not go away until I ctrl-d it.
Not sure what's going on here?...
Thanks.
I don't think you can pass shell stuff into xterm's -e option. It wants a program and optional parameters for that program. source is a shell built-in.
Something you can try doing is just calling your myscript.sh and modifying it so that it sources your bashrc at the top. Or create a new bash script that sources your rc file then runs myscript.sh.
Related
I'm using Intellij IDEA and i'm trying to run a shell script with arguments, and read the result of the execution.
this script is on my java SRC packge,
myScript.sh run a compiled c program
String[] cmd = { "/bin/bash", "-c", "myScript" };
Runtime.getRuntime().exec(cmd);
i resolved this by making a copy of myScript.sh in /ect/bin.
so this make my script as an environment path and give me the ability to read all the out put or add supplement arguments.
no changes has been made
on my Java code
.
Use ProcessBuilder:
Process process = new ProcessBuilder(cmd).start();
Then process.getInputStream() gives you access to the process standard output (stdout) which you can read as usual; process.getErrorStream() allows to read standard error (stderr).
Also you can do process.waitFor() to wait for the project to finish.
Note that to read anything from the stdout of a process you need to wait for the process to finish (or read it in a loop) and not just finish your main program.
I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.
Now I want to do the same in Java by the use of:
static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
I know that source is an internal command and I can not call it from Java.
Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?
Thank you in advance!
Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();
Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.
I would like to check whether a jar of mine is running on the users system, to then relaunch if it is closed.
I am aware of the command jps -l which makes it possible to check the current running jars. Only problem is that for that line to work, it requires the user to have a JDK installed. So I was then wondering whether or not there is an equivalent to the jps -l line, which doesn't need a JDK or anything, but just checks whether a specific jar is running.
In the past I have also used the line cmd /c tasklist for Windows and the line top -F -R -o cpu for Mac. To check whether an app or exe was running, but that doesn't really seem to be working. When running the tasklist line on Windows and I then check for an exe called "myApp", it doesn't find it. Even though it might be running. Otherwise this would have been a perfect method, to check for a running app, exe or jar.
Here is an example code of how I tried to use the tasklist command to check for a specific executable.
try {
String procss;
Process pRun = Runtime.getRuntime().exec("cmd /c tasklist");
BufferedReader input = new BufferedReader(new InputStreamReader(pRun.getInputStream()));
while ((procss = input.readLine()) != null) {
if(!procss.contains("myApp"))
{
//Runtime command to launch exe or app.
}
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
Basically I would like to just edit the code above, to have a command line, of which is able to actually check whether the exe, app or jar is running. Maybe there is an alternative to cmd /c tasklist and top -F -R -o cpu, which is able to get all processes running on a pc and not just .exe or .app
On windows, you could use the wmic command to get the command line parameters a program was launched with.
For example, using wmic process where "name like '%java%'" get commandline,processid (basically just means "get the PID and command line arguments of process with a name like java") gives me this output for a test program:
616
"C:\Program Files\Java\jre1.8.0_111\bin\javaw.exe" -jar "A:\Programmering\Java\Pong\out\artifacts\Pong_jar\Pong.jar"
As you can see, you can get the location of the jar file which is running (which you could then use to check if it's your program). In this case, I just launched it by double clicking the jar file, you may get different outputs if you launch it in a different way, but there should always be something you can use to identify the java process (like a main class or jar file).
I'm beginner at java and have some problems. I've read several topics about this theme but none of them worked for me. Here is my code:
try
{
Console console = System.console();
String command;
while(true)
{
command = console.readLine("Enter input:");
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
}
}
catch(Exception e) {}
So what I'm trying is to make a java program and run terminal commands in it(I'm using linux). This program works with commands like "ls" "ps ef" and others but it doesn't work when I type "cd". I know that cd makes different process and should be used this way: "Runtime.exec(String command, String[] envp, File dir)". My questions is:
How to make my program run all kinds of terminal commands? Sorry if question sound silly. Thank you.
The cd command is a shell built-in command. There is no shell when you run a command via exec(...). Indeed, if you try to find a cd command in any of your system's bin directories, you won't find one ... because it is impossible to implement as a regular command.
If you are trying to use cd to change the current directory for the JVM itself, that won't work because a command can only change the current directory of itself and (after that) commands that it launches itself. It can't change its parent processes current directory.
If you are trying to use cd to change the current directory for subsequent commands, that won't work either. The context in which you set the current directory ends when the command finishes.
In fact, the right way to change the directory for a command run using exec is to set it via the ProcessBuilder API itself.
How to make my program run all kinds of terminal commands?
You can't. Some of the "terminal commands" only make sense as shell commands, and that means you need a shell.
I suppose, you could consider emulating the required behaviour in your Java code. That would work for cd ... but other commands are likely to be more difficult to cope with.
(For what it is worth, it is possible to implement a POSIX compatible shell in Java. It is just a LOT of work.)
you've actually got to run the console you want to use (ie sh, csh, bash, etc) and then use the process OutputStream to feed in commands
I think the Problem is not your Code, the command is the problem...
what do you want to see if your command is cd ??
In Background it changes the path but you get nothing back.
Changing the Directory is not processing any output.
This worked for me:
Runtime.getRuntime().exec(new String[]{ "/system/bin/sh", "-c", "ls -l" } );
I'm trying to, through a java program, write commands in the gnome terminal. I tried this code:
String cmd = "ls";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
I'm trying to write "ls" to the terminal but nothing happens, but if I use
String cmd = "gnome-terminal";
I can open a new terminal window.
What I really want to do is run a C program from the terminal, calling it with java.
Thanks in advance.
gnome-terminal takes the -e argument, which allows you to tell it to execute a program.
gnome-terminal -e /path/executable
Just put them in a String[] and call the same method.
executing an external program works for me with the following commands:
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ls -l");
InputStream in = proc.getInputStream();
OutputStream out = proc.getOutputStream();
InputStream err = proc.getErrorStream();
proc.destroy() ;
}
Or, something similar is solved here: Executing in java code an external program that takes arguments as well.
If you're starting your java program from within a terminal then, after you've called exec() on the runtime, and get a Process you need to call the getInputStream() to read the output of the command, then you can print it out to System.out.
How I can read in this forum:
http://www.linuxquestions.org/questions/linux-general-1/run-command-in-new-gnome-terminal-185216/
you can use
gnome-terminal -x sh -c "ls"
to open a terminal and execute "ls" (if i remember, the "-c" option can execute a program in a new terminal.)
In this moment I'm at work and I haven't linux X system to try here. Sorry :)
I hope that this can help you!