无法从 java 运行 ksh 脚本:java.io.IOException: .: not found [英] unable to run ksh script from java: java.io.IOException: .: not found

查看:62
本文介绍了无法从 java 运行 ksh 脚本:java.io.IOException: .: not found的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 java 代码运行 shell 脚本,但我遇到了问题.脚本在 batchstart.sh 文件中 -

#!/bin/ksh出口显示=:0.0

现在脚本在命令行上运行一个点 -- .批处理启动.sh

我如何从 Java 运行它?我的java代码如下.它抛出以下异常 -

<前>java.io.IOException: .: 未找到在 java.lang.UNIXProcess.forkAndExec(本机方法)在 java.lang.UNIXProcess.(UNIXProcess.java:102)在 java.lang.ProcessImpl.start(ProcessImpl.java:65)在 java.lang.ProcessBuilder.start(ProcessBuilder.java:451)在 java.lang.Runtime.exec(Runtime.java:591)在 java.lang.Runtime.exec(Runtime.java:429)在 SetDisplay.main(SetDisplay.java:12)

import java.io.*;公共类 SetDisplay {公共静态无效主(字符串 [] args){File wd = new File("/myhomedir/");System.out.println("工作目录:" +wd);进程 proc = null;尝试 {proc = Runtime.getRuntime().exec(".batchstart.sh", null, wd);} 捕获(异常 e){e.printStackTrace();}}}

如何让 shell 脚本运行?

我也尝试了以下代码,但这也不起作用.

File wd = new File("/bin");System.out.println(wd);进程 proc = null;尝试 {proc = Runtime.getRuntime().exec("/bin/bash", null, wd);}捕获(IOException e){e.printStackTrace();}如果(过程!= 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("退出");尝试 {字符串线;while ((line = in.readLine()) != null) {System.out.println(line);}proc.waitFor();附寄();关闭();proc.destroy();}捕获(异常 e){e.printStackTrace();}}

解决方案

源命令 (".") 是一个内置的 shell.您必须显式运行 /bin/ksh,将您的脚本名称作为参数传递(后跟任何脚本参数).

如果您需要获取脚本的源代码,那么您会遇到更大的问题.这通常意味着环境更改发生在当前 shell 的上下文中,而不是子 shell.

这不适用于 Java,因为 Java 不是 shell.您需要弄清楚如何使用 Java 更改环境.

当然,如果我错了,并且该脚本还有更多内容只是设置 DISPLAY,它可能会按建议工作.

您将不得不使用的方法取决于您要实现的目标(如您是否正在使用依赖 DISPLAY 设置的 exec() 运行其他程序?"或您的 Java 程序是否需要要设置显示?").

如果,如您在评论中所述,只有您的 Java 程序需要设置 DISPLAY,请在程序运行之前将其设置在外部.创建一个 cmd(或 bash)文件,该文件设置 DISPLAY 变量然后调用 JRE 来运行您的程序.

#/bin/ksh导出显示-:0.0/usr/bin/jre/java your_program blah.blah.blah

我还会修改您的 main() 以检查它是否已设置为某些内容,如果没有则正常退出:

if (System.getenv ("DISPLAY") == null)//不存在,优雅退出.

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(); 
  } 
}

解决方案

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.

这篇关于无法从 java 运行 ksh 脚本:java.io.IOException: .: not found的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆