从Java执行但不是从命令行执行NoSuchFieldError

时间:2017-12-04 18:31:01

标签: java bash hive environment-variables processbuilder

是否可以让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 个答案:

答案 0 :(得分:1)

  

是否可以让java使用命令行的环境而不是自己的环境?

这取决于“命令行环境”的含义。如果你的意思是狭义上从变量名到值的映射,那么

  • 启动VM的java命令从启动它的命令获取其环境。更有可能的是“命令行的环境”。

  • 除非您采取行动产生不同的效果(并且所呈现的代码没有),您在ProcessBuilder的帮助下启动的流程将继承VM环境的副本。

    < / LI>

但我认为你正在咆哮错误的树。虽然可以想象环境问题可能间接地促成java.lang.NoSuchFieldError,但Error

  

如果应用程序尝试访问或修改指定字段,则抛出该异常   一个对象,并且该对象不再具有该字段。

     

通常,编译器会捕获此错误;这个错误只能   如果类的定义不兼容,则在运行时发生   改变。

API docs

这表明错误的近端原因是您的运行时类路径中的类不匹配。此上下文中的 Field 表示类的成员变量,因此Java正在寻找SASL_PROPS(尽管我不清楚哪个类应该具有该字段)。< / p>