unable to run ksh script from java: java.io.IOException: .: not found - java

I tried to run a shell script from java code, but I am facing problem. The script is in batchstart.sh file -
#!/bin/ksh
export DISPLAY=:0.0
Now the script is run with a dot on the command line -- . batchstart.sh
How do I run it from java? My java code is below. It throws the following exception -
java.io.IOException: .: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:102)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at SetDisplay.main(SetDisplay.java:12)
import java.io.*;
public class SetDisplay {
public static void main(String[] args) {
File wd = new File("/myhomedir/");
System.out.println("Working Directory: " +wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How do I make the shell script run ?
I tried the following code as well, but that too doesn't work.
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /home/");
out.println(". batchstart.sh");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}

When run from the command line, using a dot at the start of a script indicates that the script should be run in the current environment, instead of spawning a new subshell and using a new copy of the current environment. This allows you to export a new value of an environment variable to be used by commands run later from the same interactive shell.
Obviously, this technique only works if you are running your batchstart.sh script from an actual shell. Java does not know how this mechanism works and so the dot means nothing to it. A script cannot modify the environment of the Java process it was called from.
If your goal is to change the value of the DISPLAY environment variable for other commands run by your Java process, consider using the ProcessBuilder class to specify a new environment for the child process. Java does not contain a built-in way to modify variables in its own environment.

The source command (".") is a shell built-in. You have to explicitly run /bin/ksh, passing your script name as the argument (followed by any script arguments).
You have a larger problem if you need to source the script. That usually means that environment changes happen in the context of the current shell, not a subshell.
This won't work with Java since Java's not a shell. You'll need to figure out how to change the environment with Java.
Of course, if I'm wrong and there's more to that script that just setting DISPLAY, it may work as suggested.
The method you're going to have to use depends on what you're trying to achieve(as in "Are you running other programs using exec() that rely on DISPLAY being set?" or "Does your Java program need DISPLAY to be set?").
If, as you state in your comment, it's only your Java program that needs DISPLAY set, just set it outside before your program runs. Create a cmd (or bash) file which sets the DISPLAY variable then calls the JRE to run your program.
#/bin/ksh
export DISPLAY-:0.0
/usr/bin/jre/java your_program blah.blah.blah
I would also modify your main() to check that it's set to something and exit gracefully if not:
if (System.getenv ("DISPLAY") == null)
// doesn't exist, exit gracefully.

The period "." is a shell built-in, and executes the script "in-place", analogous to #include in C/C++. Using "." outside of a shell-script has no meaning.
If you want to run the script from Java, you have to execute the script interpreter (/bin/ksh):
Runtime.getRuntime().exec("/bin/ksh batchstart.sh", ...)
but note that this is not semantically equivalent, since you're executing batchstart.sh as a sub-process instead of sourcing it.

Related

Read from already open CMD - java

Is there anyway that a java application can read from an already open CMD window. For example, I've opened a cmd, ran "dir" and then decided to run my application, is there any way my application can read the information that's already in the open CMD window without opening another?
Thanks in advance
UPDATE:
My hope was that when one java application was ran, it would check any currently opened CMD windows to ensure that another one of my command-line applications hadn't been ran before it opened it itself.
Just spool the output of the executed command in a file.
For example : do dir > spool.txt
and from the java program open the spool.txt file with a FileInputStream.
To be sure that the content is totally written before reading it, you could for example :
use a marker in the written file to indicate it
check the modification date of the written file (if the modification date doesn't change during a defined period, it may be the signal that the writing is finished)
Update with updated question : checking whether a CMD windows is opened
UPDATE: My hope was that when one java application was ran, it would
check any currently opened CMD windows to ensure that another one of
my command-line applications hadn't been ran before it opened it
itself.
You could simply list the running processes of Windows by using the tasklist.exe program.
The information is retrieved with one line by process.
Then, check if one of the lines starts with cmd.exe.
If it is the case, it means a cmd.exe program is already running.
Here is a sample code ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CmdProcessSearch {
public static void main(String[] args) throws IOException {
boolean anyCmdProgramOpened = isAnyCmdProgramOpened();
System.out.println("isOpened = " + anyCmdProgramOpened);
}
public static boolean isAnyCmdProgramOpened() {
Process p;
try {
p = Runtime.getRuntime().exec(System.getenv("windir") + "\\system32\\" + "tasklist.exe");
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
if (line.startsWith("cmd.exe")) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
The C:\windows\system32 is usually in the PATH environment variable.
So, p = Runtime.getRuntime().exec("tasklist.exe"); should be enough
but to prevent any side effect or misconfiguration problem, specifying the absolute path is better.
New update with new comment : checking whether the Java application is running
I have used this code for a different section but if there is a cmd
window running, there is no guarantee that the user has used it to run
my program, package process;
In this case, if you can install jdk tools on the client machine you could check if is running a Java process using as main class, the main class of your application.
You could use jps (Java Virtual Machine Process Status Tool).
Here is the available options :
OPTIONS
The jps command supports a number of options that modify the
output of the command. These options are subject to change or removal
in the future.
-q
Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM
identifiers.
-m
Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l
Output the full package name for the application's main class or the full path name to the application's JAR file.
-v
Output the arguments passed to the JVM.
-V
Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags=
argument).
-Joption
Pass option to the java launcher called by jps. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying VM executing
applications written in Java.
jps -l command that outputs the full package name for the application's main class or the full path name to the application's JAR file.
Here is a sample code by searching the main class of the application process.JavaProcessSearch :
package process;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaProcessSearch {
public static void main(String[] args) throws IOException {
boolean isProcessRunning = isAJavaProcessWithMainClass("process.JavaProcessSearch");
System.out.println(isProcessRunning);
}
public static boolean isAJavaProcessWithMainClass(String mainClass) {
Process p;
try {
String javaHome = System.getenv("JAVA_HOME");
p = Runtime.getRuntime().exec(javaHome + File.separator + "bin/jps -l");
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
if (line.contains(mainClass)) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
If you cannot install jdk tools, you could store "somewhere" on the client machine, the process id of the application when it is started.
You can retrieve this information with the following method : ManagementFactory.getRuntimeMXBean().getName().
When it is not any longer running (stopped or crashed), you could remove this process id information.
When the application ia started, if a process id is retrieved on the client machine, you don't allow the application to start.
For stopped application case, shutdown hook should do the job.
For crashed application case, you could use a deamon thread that checks on a regular basis whether the application is always running by relying on the stored process id.

Running a shell script from Java returning "command not found"

I've created a java GUI using NetBeans v.8.2. Very new to Java.
One of the buttons in the GUI launches a shell script (I am aware that this is not ideal Java practice, but it is appropriate for my use case) using arguments gathered from other buttons/text fields in the GUI:
```
private void RunMacActionPerformed(java.awt.event.ActionEvent evt) {
String command[] = {scriptDirStr + "/./Master_run.sh",
projDirStr+"/",
DestDirStr+"/",
ECnonNormStr,
ECnormStr,
ProjID.getText(),
scriptDirStr +"/"};
System.out.print(Arrays.toString(command));
ProcessBuilder pb = new ProcessBuilder(command);
try {
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
```
So, the idea is to launch Master_run.sh with a bunch of arguments.
Master_run.sh runs other R and python scripts, e.g.:
python2 $script_Path/array_data_extractor.py $spath >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt
and
Rscript $script_Path/1_APS_generator_master.R $spath $dpath $APS_src_filename $project_ID $APS_norm_src_filename >>$spath/masterOutput.txt 2>>$spath/masterErrors.txt
and ends with
cat $spath/masterErrors.txt| mail -s $Project_title" done" myEmailAddress#gmail.com
I know the script gets launched because I get an email with the following errors:
"...line 14: python2: command not found"
and
"...line 16: Rscript: command not found"
When I run Master_run.sh with the same exact arguments from within the terminal, there are no such errors. Does anybody know what might be going wrong and/or how to fix it?
To rephrase the problem, it seems I am getting different behavior launching the same commands from within java vs. directly onto the command line.
Your shell environment is clearly different from java's environment. Try specifying the full path to python2 and Rscript. For example
/usr/local/bin/python2 $script_Path/array_data_extractor.py ...

Using Java's getRuntime.exec() to Run a Linux Shell Command: How?

Below is a python script that executes a linux bash command "echo Hello World > ./output"
import os
os.system("bash -c \"echo Hello World > ./output\"");
I am trying to do the same with Java. Below is my best effort, following the instructions I found here: Want to invoke a linux shell command from Java
import java.io.IOException;
public class callCommand {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec(
new String[]{"bash","-c",
"\"echo Hello World > ./output\""});
} catch(IOException e) {
e.printStackTrace();
}
}
}
It compiles without issue, and runs without complaint, but no output file is generated.
The extra quotes around echo ... should be removed:
Process p = Runtime.getRuntime().exec(new String[]{
"bash", "-c",
"echo Hello World > ./output"
});
The python version needs extra quotes to tell the underlying system that echo Hello World > ./output is a single argument. The java version explicitly specifies arguments as separate strings, so it doesn't need those quotes.
Also, your version doesn't "run without complaint", you just don't see the complaints, because you don't read the error stream of the created process.
The standard input, output and error streams to/from a system process started from Java are accessed through the methods getOutputStream(), getInputStream() and getErrorStream() of Process.
I recommend you to get the error output produced by your system process:
Process p = Runtime.getRuntime().exec(...);
InputStream input=p.getErrorStream();
do
{
n=input.read(...);
}
while (n>=0);
Be careful: For your actual problem, this would be enough. But for a process which produces a longer error/output, you need to perform the reading of the standard error/output in a separate thread. If not, the system process would block when the error/output buffer is full, and wait till it is externally consumed, and if you place the reading loop just after the process is executed, it will never execute and so, the program will get into a deadlock.

Running UNIX Source command in Java

This is my first question on stackoverflow so I'll try to keep it concise and relevant.
I'm currently creating a Java program that is attempting to call an external program located on the system, in order to do this however I am required to call a shell script that sets up links to the relevant libraries to ensure that the system is linked to these before the external program can be executed.
The issue at hand is that I cannot invoke the shell script through Java, I've researched high and low and am aware that of alternative ways such as the use of the ProcessBuilder class. Unfortunately I'm quite new to the world of trying to invoke command line statements through Java and so I'm stuck for answers.
An example of the code I am using can be found below:
private void analyse_JButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Get project path for copying of Fortran program to folder and execution
String projectPath = Newproject_GUI.getProjectPath();
String sourcePath [] = {"/bin/sh ", "-c ","source ~/set_env_WRF_gnu.sh"} ;
Runtime fortranAnalyser = Runtime.getRuntime();
try {
Process p = fortranAnalyser.exec("cp main.exe " + projectPath);
Process k = fortranAnalyser.exec(sourcePath);
BufferedReader is = new BufferedReader(new InputStreamReader(k.getInputStream()));
String line;
while ((line = is.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(Analyser_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
Process p works fine and does indeed copy main.exe to the intended directory when the method is called. Process k however does not and this is where the issue is.
Thanks in advance.
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.

Problem with starting OpenOffice service (soffice) from Java (command working in commandline, but not from Java)

I want to exceute a simple command which works from the shell but doesn't work from Java.
This is the command I want to execute, which works fine:
soffice -headless "-accept=socket,host=localhost,port=8100;urp;"
This is the code I am excecuting from Java trying to run this command:
String[] commands = new String[] {"soffice","-headless","\"-accept=socket,host=localhost,port=8100;urp;\""};
Process process = Runtime.getRuntime().exec(commands)
int code = process.waitFor();
if(code == 0)
System.out.println("Commands executed successfully");
When I run this program I get "Commands executed successfully".
However the process is not running when the program finishes.
Is it possible that the JVM kills the program after it has run?
Why doesn't this work?
I'm not sure if I'm not mistaken, but as far as I see you're generating the commands but never passing them to the "execute" method... you're executing "".
Try using Runtime.getRuntime().exec(commands) =)
I would like to say how I solved this.
I created a sh script that basically run the command of soffice for me.
Then from Java I just run the script, and it works fine, like this:
public void startSOfficeService() throws InterruptedException, IOException {
//First we need to check if the soffice process is running
String commands = "pgrep soffice";
Process process = Runtime.getRuntime().exec(commands);
//Need to wait for this command to execute
int code = process.waitFor();
//If we get anything back from readLine, then we know the process is running
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() == null) {
//Nothing back, then we should execute the process
process = Runtime.getRuntime().exec("/etc/init.d/soffice.sh");
code = process.waitFor();
log.debug("soffice script started");
} else {
log.debug("soffice script is already running");
}
in.close();
}
I also kill the soffice process by calling this method:
public void killSOfficeProcess() throws IOException {
if (System.getProperty("os.name").matches(("(?i).*Linux.*"))) {
Runtime.getRuntime().exec("pkill soffice");
}
}
Note that this only works in Linux.
I believe you aren't handling quoting correctly. The original sh command line includes double quotes to prevent the shell interpreting the semicolons. The shell strips them off before the soffice process sees them.
In your Java code the shell will never see the arguments, so the extra double quotes (escaped with backslashes) are not needed - and they are probably confusing soffice.
Here's the code with the extra quotes stripped out (and a semicolon thrown in)
String[] commands = new String[] {"soffice","-headless","-accept=socket,host=localhost,port=8100;urp;"};
Process process = Runtime.getRuntime().exec(commands);
int code = process.waitFor();
if(code == 0)
System.out.println("Commands executed successfully");
(Disclaimer: I don't know Java, and I haven't tested this!)
"/Applications/OpenOffice.org\ 2.4.app/Contents/MacOS/soffice.bin -headless -nofirststartwizard -accept='socket,host=localhost,port=8100;urp;StartOffice.Service'"
or simply escaping the quotes will work as well. We feed a command like this to an ant script that ultimately ends up in an exec call like you have above. I would also recommend restarting the process every 500 or so conversions because OOO does not properly free memory (depending on what version you are running).

Categories

Resources