0 Linux下Java使用ProcessBuilder执行命令与直接Bash执行命令之间的不同(环境变量方面)...

0 问题发生

xiaojietest.java

package tasks;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.SystemUtils;

import database.Tools;
import util.FixPath;
import util.StreamGobbler;

public class xiaojietest {
	public static void main(String args[]) throws SQLException {
		try {
			String cmd="\""+"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner"+ "\" \"" + "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"+"\"";
			System.out.println(cmd);
			String cmd1="bash";
			//String cmd2="--help";
			String cmd2="-c";
			//String [] exec = {cmd1,cmd2};
			//String [] exec = {"bash", "-c", "\""+"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/commands/../sample/nicadRunner"+ " " + "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"+"\""};
			//String [] exec = {"bash", "--help"};
			//String [] exec = {"bash", "-c",cmd};
			String [] exec = {cmd1,cmd2,cmd};
			//String[] exec= {"ls"};
			ProcessBuilder pb = new ProcessBuilder(exec);
			//pb.directory(new File("/home/xiaojie/Desktop/xiaojiework/BigCloneEval/commands/../sample/"));
			Process p = pb.start();
			Map<String, String>env=pb.environment();
			//xiaojie output environment
			Set<String> key=env.keySet();
			for(Iterator<String>it=key.iterator();it.hasNext();) {
				String s=it.next();
				System.out.println(s+":"+env.get(s));
			}
			//new StreamGobbler(p.getErrorStream()).start();
			String line = null;
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			Path output=Paths.get("/home/xiaojie/Desktop/xiaojiework/data_for_experiment/nicadOutPutFile/nicad.clones");
			
			output = FixPath.getAbsolutePath(output);
			output = output.toAbsolutePath();
			BufferedWriter out = new BufferedWriter(new FileWriter(output.toFile()));
			//System.out.println(br.read());
			while((line = br.readLine()) != null) {
				System.out.println(line);
				line = line.trim();
				if(!line.equals("")) {
					out.write(line + "\n");
				}
			}
			int retval = p.waitFor();
			br.close();
			System.out.println("retval:"+retval);
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}
}

上述代码期望通过Java程序执行如下脚本

/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner

并且传入参数:

/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5

nicadRunner的脚本内容是:

#!/bin/bash

# This tool runner works with the myconfig.cfg nicad configuration file included
# You will need to modify the hard-coded installation below before running
# Test this out on one of the IJaDataset directories (such as 11/) to test and 
# see that clones are detected and output in the correct format for BigCloneEval
# as specified in the readme.

ulimit -s hard

root=`dirname $1`
dir=`basename $1`
path=$root/$dir

# Go to NiCad installation directory
cd /home/xiaojie/Desktop/xiaojiework/NiCad-5.0/

# Execute NiCad, Suppress Output
./nicad5 functions java "$path" myconfig > /dev/null 2> /dev/null

# Convert Detected Clones Into BigCloneEval Format
java -jar Convert.jar ${path}_functions-blind-abstract-clones/${dir}_functions-blind-abstract-clones-0.30.xml 2> /dev/null

#cat ${path}_functions-blind-abstract-clones/${dir}_functions-blind-abstract-clones-0.30.xml | sed 's$<source file="$$g' | sed 's$" startline="$,$g' | sed 's$" endline="$,$g' | sed 's$" pcid=.*"></source>$$g' | sed 's$<clone nlines=.*$$g' | sed 's$</clone>.*$$g' | sed 's$</clones>$$g' |sed 's$<clones>$$g' | sed 's$<cloneinfo.*$$g' | sed 's$<systeminfo.*$$g' | sed 's$<runinfo.*$$g' | sed '/^$/d' | paste -d ',' - - | sed "s#${path}/##g" | sed 's#/#,#g'

# Cleanup
rm -rf ${path}_functions-blind-abstract-clones > /dev/null 2> /dev/null
rm ${path}_functions-blind-abstract.xml > /dev/null 2> /dev/null
rm ${path}_functions-clones*.log > /dev/null 2> /dev/null
rm ${path}_functions-blind.xml > /dev/null 2> /dev/null
rm ${path}_functions.xml > /dev/null 2> /dev/null

  ProcessBuilder启动进程并执行,正常的返回值(通过代码中p.waitFor()返回)是0,其余状态都说明进程执行过程报错。

针对"ls"、"bash --help"等使用上面程序执行,都无错误。

但是针对如下进程使用上述程序通过ProcessBuilder启动进程执行却一直报错:

bash -c "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner" "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"

1 问题排查过程

1.1 bash -c的直接使用

首先,直接运行脚本,传入参数。没有任何错误。

 

其次,加上bash –c以后就会出错。

这是因为必须将"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner" "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"作为整体传递给bash -c,而不是分开。所以如下修改即可:

1.2 通过ProcessBuilder启动进程执行bash -c

问题1:返回127错误码

String cmd="\""+"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner"+ "\" \"" + "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"+"\"";
System.out.println(cmd);
String cmd1="bash";
//String cmd2="--help";
String cmd2="-c";
String [] exec = {cmd1,cmd2,cmd};
ProcessBuilder pb = new ProcessBuilder(exec);

第一,如果将cmd写作

"\"\""+"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner"+ "\" \"" + "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"+"\"\"";

将该字符串打印以后会输出:

""/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner" "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5""

从表面看,是bash -c能够接受的参数,即是一个整体。

这个时候,bash -c 将“”引号内作为一个整体看待,而不是一个脚本和一个参数,故而会提示127。

但是,对于ProcessBuilder而言,其接收该参数对其处理时,会将其当作最外层还有一层双引号。就变成了

"""/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner" "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"""

会报出127错误。

shell的错误码说明是:

因此,找不到nicadRunner脚本的路径。即Path不对。

问题2:返回1错误码

 我们将问题1修正以后,即cmd为

String cmd="\""+"/home/xiaojie/Desktop/xiaojiework/BigCloneEval/sample/nicadRunner"+ "\" \"" + "/home/xiaojie/Desktop/xiaojiework/BigCloneEval/ijadataset/bcb_reduced/5"+"\"";

此时,执行前述程序后,返回1错误码,即通用错误。

说明nicadRunner脚本运行了,但是没有正确运行。

采取“逐步增加行”的策略。我们运行到./nicad5的那一行时出了错误。

#!/bin/bash

# This tool runner works with the myconfig.cfg nicad configuration file included
# You will need to modify the hard-coded installation below before running
# Test this out on one of the IJaDataset directories (such as 11/) to test and 
# see that clones are detected and output in the correct format for BigCloneEval
# as specified in the readme.

ulimit -s hard

root=`dirname $1`
dir=`basename $1`
path=$root/$dir

# Go to NiCad installation directory
cd /home/xiaojie/Desktop/xiaojiework/NiCad-5.0/

# Execute NiCad, Suppress Output
./nicad5 functions java "$path" myconfig > /dev/null 2> /dev/null

而直接在客户端的命令行中用nicad命令执行,却没有错误。

为什么?

在客户端的命令行中运行nicad程序,命令行中的上下文的path是包括FreeTXL路径的,所以命令行nicad没问题。

但是,使用ProcessBuilder启动bash -c 运行nicadRunner脚本,脚本中再调用nicad程序的时候,就找不到FreeTXL路径了!

我的FreeTXL路径设置的比较特殊,是在一个私人文件夹,并且写入的path是~/.bashrc。FreeTXL是nicad工具的依赖包。我安装的时候,相关的路径信息是:

Installing TXL for xiaojie only.

Installing TXL commands into /home/xiaojie/bin

Installing TXL library into /home/xiaojie/txl/lib

Installing TXL manual entries into /home/xiaojie/txl/man/man1

Testing TXL

我在程序中添加代码:

            Map<String, String>env=pb.environment();
            //xiaojie output environment
            Set<String> key=env.keySet();
            for(Iterator<String>it=key.iterator();it.hasNext();) {
                String s=it.next();
                System.out.println(s+":"+env.get(s));
            }

获取了ProcessBuilder启动的命令的上下文,然后查看一下输出

PATH:/home/xiaojie/Desktop/xiaojiework/jdk-8u191-linux-x64/jdk1.8.0_191/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

可以看到,/home/xiaojie/bin不在PATH路径中。

我当时将这个路径写入了一个bashrc文件,所以bash命令行中有该路径!

 

但是,创建ProcessBuilder时,读取的上下文,不是从bashrc中读取的。

所以不一致,所以就会导致命令行和程序中的不一样。

调试程序,跟入pb.environment();,可以看到:

进一步跟入,发现:

该函数只有一个声明。

 

初步判断是java.lang.ProcessEnvironment.environ函数。java.lang.ProcessEnvironment是java的一个类,源码被隐藏。可以直接调用。

https://www.ibm.com/developerworks/cn/java/java-random-code-from-the-perspective-of-compilation/。该网页中有Linux环境变量的读取介绍,比较复杂。

https://www.cnblogs.com/sunilsun/p/6071124.html这里是Linux环境变量的介绍。

里面提到:

(1)~/.profile:【推荐】每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。这里是推荐放置个人设置的地方
(2)~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取。不推荐放到这儿,因为每开一个shell,这个文件会读取一次,效率肯定有影响。

所以,我将路径改为写入~/.profile。重启,然后运行程序。解决。

 

 

 

 



 

 

 

转载于:https://www.cnblogs.com/xiaojieshisilang/p/10178017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值