Execute Source Command from Java

Execute source command from Java


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.

Source command not working through Java

According to the Javadoc, Runtime.exec(String) breaks the command into the command-args list using a StringTokenizer, which will probably break your command into:

bash
-c
'source
activate
abc_env'

Which is obviously not what you want. What you should do is probably use the version of Runtime.exec(String[]) that accepts a ready list of arguments, passing to it new String[] {"bash", "-c", "source activate abc_env"}.

Now, to get an idea why it's not working, you should not only read from its stdout but also from stderr, using p.getErrorStream(). Just print out what you read, and it will be a great debugging aid.

Re: your edit. Now it looks like it's working fine, as far as Java and bash are concerned. The output "activate: No such file or directory" is probably the output from the successful run of the source command. It just that source can't find the activate file. Is it in the working directory? If not, you probably should have "cd /wherever/your/files/are; source activate cvxpy_env". Oh, and if your python script depends on whatever side-effects the source command has, you probably have to execute it in the same bash instance, that is:

String[] command = {
"/bin/bash",
"-c",
"cd /wherever/your/files/are && source activate cvxpy_env && python example.py"
};

Or better yet, pack it all into a single shell script, and then just Runtime.exec("./my_script.sh") (don't forget to chmod +x it, though).

Running UNIX Source command in Java

The issue is "source" is internal command of BASH (you are using "sh" but that is just BASH in the simplified mode). So what you do is:

  • you spawn new process "sh" and source something there (setting some VARIABLES I guess)
  • the process ends and all VARIABLES are lost
  • you spawn another process, but VARIABLES are already gone

I am not sure if you use those variables later on, but according to the script name it is probably setting some. Don't do that like this.

By the way if you only want to execute script in bash, you don't need to source it. To get it's side effects, just execute it with:

String sourcePath [] = {"/bin/sh ", "/home/XYZ/set_env_WRF_gnu.sh"} ;

Please note you cannot use ~ in this case, use Java to get your home dir.

How to execute BASH source command from scala 2.11?

I think this is the equivalent of what source does.

import scala.sys.process._

//send file contents to sh for interpretation
val tester = "/bin/cat /path/trial.sh".#|("/bin/sh").lineStream

Of course, things are much easier if trial.sh is executable.

val tester = "/bin/sh -c /path/trial.sh".lineStream

How to load a file using source command in ANT script?

I think you will need to make a wrapper script for Ant to invoke. In the wrapper script, execute the "source" command and then the "sources" command. (You could pass parameters for the file to source and to execute).

Follow up

For the wrapper script, I just mean something like this:

#!/bin/bash

env_file=$1
script_to_exec=$2

. $env_file
exec $script_to_exec

The point being that you need to source a file and then execute a script in the same environment. So wrap those up into a script which you can execute from a different environment (Ant).

To invoke that from Ant, something like this:

    <exec executable="wrapper_script">
<arg value="${release.path}"/>
<arg value="script_to_execute"/>
</exec>

How can I run a script file using the source command?

You cannot do this with the JDBC driver. source is only a command supported by the MySQL command line tool. See here:

http://forums.mysql.com/read.php?39,406094,406329#msg-406329

Here's the list of commands for the command-line tool. Most are not supported as JDBC query statements.

http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html

You will have to load your SQL commands from the file in your code and send them to JDBC execute methods. Something like:

Statement stm = con.createStatement();
BufferedReader reader = new BufferedReader(new FileReader(new File(...)));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
// this is the trick -- you need to pass different SQL to different methods
if (line.startsWith("SELECT")) {
stm.executeQuery(line);
} else if (line.startsWith("UPDATE") || line.startsWith("INSERT")
|| line.startsWith("DELETE")) {
stm.executeUpdate(line);
} else {
stm.execute(line);
}
}
stm.close();

How to call the linux command source FILE_NAME from Java?

You're probably executing two separate exec commands, spawning separate processes, and so whatever you do in the first process is not visible to the second. Resolve this by putting all of your commands into a script (bash, ksh, etc) and call it once from your Java program.

Paramterize your script so you can pass arguments.

Here's some help on writing your first shell script


[Edit] As mentioned by @RNJ you can look at using ProcessBuilder to pass in environment variables to each of the processes spawned. This will be fine if you can specify the name of the file being created ahead of time. Example code taken from the API link above...

 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();


Related Topics



Leave a reply



Submit