简体   繁体   English

无法在服务器上使用Java程序执行简单的“ whoami” unix命令

[英]Unable to execute a simple “whoami” unix command using java program on the server

Im facing this strange issue of not being able to execute a simple "whoami" unix command on a AIX server. 我面临着一个奇怪的问题,即无法在AIX服务器上执行简单的“ whoami” unix命令。 I have a webapplication that is deployed on an AIX server. 我有一个部署在AIX服务器上的Web应用程序。 Now I want to see under which WAS user my webapplication is currently running. 现在,我想查看我的Web应用程序当前正在哪个WAS用户下运行。 So I added the below code: 所以我添加了以下代码:

    public String whoami() throws Exception {
        Process p = Runtime.getRuntime().exec("whoami");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        String output = "";

        while ((line = in.readLine()) != null) {
            //System.out.println(line);
            output += line;
        }
        in.close();
        p.destroy();
        return output;
    }
}

The above code is added in a jar file which is referred by a JSP. 上面的代码被添加到JSP引用的jar文件中。 The JSP has to receive the output of the code above and it displays the WAS User name. JSP必须接收上面代码的输出,并显示WAS用户名。 But when i deploy the webapplication on the server and try to observe the output, im getting an error message like 但是当我在服务器上部署Web应用程序并尝试观察输出时,我收到了类似的错误消息

Error 500: access denied (java.io.FilePermission <> execute) 错误500:访问被拒绝(java.io.FilePermission <>执行)

However, When I remove the above code and run my webapplication, everything runs fine. 但是,当我删除上述代码并运行Web应用程序时,一切运行正常。 What wron am i doing here. 我在这里做什么? Did I miss doing anything? 我想念什么吗? Please help. 请帮忙。 This is the first time im working on UNIX 这是我第一次在UNIX上工作

It looks like your web server has been configured with a Java security policy that prohibits executing external applications. 您的Web服务器似乎已配置了禁止执行外部应用程序的Java安全策略。

See http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html for information about Java Security Policies, and the documentation for your web server. 有关Java安全策略的信息以及Web服务器的文档,请参见http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html

You will need to supply (or edit) a policy file to contain something like: 您将需要提供(或编辑)策略文件以包含以下内容:

grant {
  permission java.io.FilePermission 
    "/usr/bin/whoami", "execute";
};

Just out of curiosity Have you considered to use: 出于好奇,您是否考虑过使用:

user.name 

System property in Java? Java中的系统属性?

AFAIK whoami is a shell command and Runtime#exec() executes programs only. AFAIK whoami是一个shell命令,并且Runtime#exec()仅执行程序。

you can try Runtime.getRuntime().exec(new String[]{"sh","-c","whoami"}) to call sh and let it execute whoami 您可以尝试Runtime.getRuntime().exec(new String[]{"sh","-c","whoami"})调用sh并使其执行whoami

another thing: do you need to destroy the process after reading? 另一件事:阅读后是否需要销毁流程?

You can use the ProcessBuilder class instead of getRuntime().exec("whoami") . 您可以使用ProcessBuilder类代替getRuntime().exec("whoami")

Here is sample code 这是示例代码

import java.io.*;
import java.util.*;

public class DoProcessBuilder {

    public static void main(String args[]) throws IOException {
        if (args.length <= 0) {
            System.err.println("Need command to run");
            System.exit(-1);
        }
        Process process = new ProcessBuilder(args).start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        System.out.printf("Output of running &#37;s is:", Arrays.toString(args));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM