有没有可能让java使用命令行的环境而不是自己的环境?
我有一个调用bash脚本的java程序
final ProcessBuilder pb = new ProcessBuilder("bash", "-x", exportScriptLoc, platformId, oldWorkFlowId,
newWorkFlowId).inheritIO();
pb.directory(new File("/opt/nis/ddziak-dev/nis-hadoop"));
final Process p = pb.start();
p.waitFor();
final int exitValue = p.exitValue();
p.destroy();
if (exitValue == 0) {
当我在命令行上执行脚本时,它的行为与预期的一样。当java程序执行脚本时,会产生以下异常:
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:347)
at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:681)
at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:625)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1422)
at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:62)
at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:72)
at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:2457)
at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:2469)
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:341)
... 7 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1420)
... 12 more
Caused by: java.lang.NoSuchFieldError: SASL_PROPS
at org.apache.hadoop.hive.thrift.HadoopThriftAuthBridge20S.getHadoopSaslProperties(HadoopThriftAuthBridge20S.java:127)
at org.apache.hadoop.hive.metastore.MetaStoreUtils.getMetaStoreSaslProperties(MetaStoreUtils.java:1485)
at org.apache.hadoop.hive.metastore.HiveMetaStoreClient.open(HiveMetaStoreClient.java:322)
at org.apache.hadoop.hive.metastore.HiveMetaStoreClient.<init>(HiveMetaStoreClient.java:214)
... 17 more
剧本如下
# !/bin/bash
. ~/.bash_profile
export HIVE_HOME=/usr/lib/hive
# export PATH=$PATH:$HIVE_HOME/bin
env
echo "hive -S -e \"show partitions nis.subscribers
partition(destinationPlatformId='$1', build='$2');\"|awk '{if(NR>1)print}'" >> subscribers.out
PARTITIONS=`/usr/bin/hive -S -e "show partitions nis.subscribers
partition(destinationPlatformId='$1', build='$2');"|awk '{if(NR>1)print}'`
echo "${PARTITIONS}"
if [ -z "$PARTITIONS" ]
then
exit 2
fi
echo "${PARTITIONS}"
hqlOut=`/usr/bin/hive -S -hiveconf destPlatId=$1 -hiveconf newWorkFlowId=$3 -hiveconf oldWorkFlowId=$2 -f /opt/nis/ddziak-dev/nis-
hadoop/dapLib/updateSubscriberHiveTable.hql`
newPARTITIONS=`/usr/bin/hive -S -e "show partitions nis.subscribers partition(destinationPlatformId='$1');"|awk '{if(NR>1)print}'`
if [ -z "$newPARTITIONS" ]
then
exit 3
fi
1条答案
按热度按时间d5vmydt91#
有没有可能让java使用命令行的环境而不是自己的环境?
这取决于您所说的“命令行的环境”是什么意思。如果你的意思是从变量名到值的狭义Map,那么
这个
java
启动vm的命令从启动vm的命令获取其环境。这很可能是“命令行的环境”。除非您采取行动以产生不同的效果(并且呈现的代码没有),否则您将在
ProcessBuilder
将继承vm环境的副本。但我觉得你找错地方了。尽管可以想象,环境问题可能会间接导致
java.lang.NoSuchFieldError
,这样一个Error
是如果应用程序试图访问或修改对象的指定字段,而该对象不再具有该字段,则引发。
通常,这个错误被编译器捕获;只有在类的定义发生不兼容的更改时,才能在运行时发生此错误。
(api文件)
这表明错误的近端原因是运行时类路径中存在不匹配的类。这个上下文中的字段意味着类的成员变量,所以这就是java所要寻找的
SASL_PROPS
to be(尽管我不清楚哪个类应该有这个字段)。