java.lang.NoSuchFieldError:来自Linux终端的带有twilio的DEF_CONTENT_CHARSET

CodeMed

在从CentOS终端运行的小型Java程序中,java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET当我在java -cp .:"../dependencies/*" mainpackage.SendText xxxxxxxxxx HelloThereCentOS 7终端输入时遇到错误如何解决此错误,以便程序可以从命令行成功运行?

终端输入和输出如下:

[[email protected] bin]$ java -cp .:"../dependencies/*" mainpackage.SendText xxxxxxxxxx HelloThere
Exception in thread "main" java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET
at org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175)
at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:158)
at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:151)
at com.twilio.sdk.TwilioRestClient.<init>(TwilioRestClient.java:110)
at mainpackage.SendText.main(SendText.java:20)

其中xxxxxxxxxxHelloThere是程序的参数,如下所示。

请注意,代码的目录结构为:

/home/user/javacode                      #contains SendText.java
/home/user/javacode/bin/mainpackage/     #contains SendText.class
/home/user/javacode/dependencies         #contains jar files

我阅读了另一篇文章,指出问题是由中的的冗余版本引起httpcoreclasspath,因此我移至父目录并对所有httpcore名称相同的文件进行了递归搜索,但得到以下结果,似乎只显示了一个版本httpcore

[[email protected] bin]$ cd /home/user/javacode/
[[email protected] javacode]$ find . -name "*http*"
./dependencies/httpcore-4.1.2.jar
./dependencies/httpclient-4.2.3.jar
./dependencies/httpclient-4.2.3.jar.sha1
./dependencies/httpclient-4.2.3.pom
./dependencies/httpclient-4.2.3.pom.sha1
./dependencies/httpclient-4.1.2.jar
[[email protected] javacode]$   

引发错误的代码行是:TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);,小程序的完整代码是:

package mainpackage;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.List;

public class SendText {

  public static final String ACCOUNT_SID = "arealsid";
  public static final String AUTH_TOKEN = "arealtoken";

  public static void main(String[] args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
    String to = args[0];
    String body = args[1];
    // Build a filter for the MessageList
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("Body", body));
    params.add(new BasicNameValuePair("To", to));
    params.add(new BasicNameValuePair("From", "+11234567654"));

    MessageFactory messageFactory = client.getAccount().getMessageFactory();
    Message message = messageFactory.create(params);
    System.out.println(message.getSid());
      }
    }  

/dependencies文件夹的完整内容为

[[email protected] bin]$ cd /home/user/javacode/dependencies
[[email protected] dependencies]$ ls -al
total 39464
drwxrwxr-x 5 user user    8192 Aug 24 19:18 .
drwxrwxr-x 4 user user      76 Aug 21 22:29 ..
drwxrwxr-x 2 user user       6 Aug 22 05:05 1.2
drwxr-xr-x 2 user user       6 Aug 22 03:00 3.4.5
drwxr-xr-x 2 user user       6 Aug 22 03:01 4.2.3
-rw-r--r-- 1 user user  445288 Aug 24 19:17 antlr-2.7.7.jar
-rw-r--r-- 1 user user    4467 Aug 24 19:17 aopalliance-1.0.jar
-rw-r--r-- 1 user user  119683 Aug 24 19:17 aspectjrt-1.7.4.jar
-rw-r--r-- 1 user user 1850391 Aug 24 19:17 aspectjweaver-1.7.4.jar
-rw-r--r-- 1 user user  255692 Aug 24 19:17 bcpg-jdk15on-1.51.jar
-rw-r--r-- 1 user user 2842667 Aug 24 19:17 bcprov-jdk15on-1.51.jar
-rw-r--r-- 1 user user  140580 Aug 24 19:17 bootstrap-2.3.0.jar
-rw-r--r-- 1 user user  156396 Aug 24 19:17 common-2.2.3.jar
-rw-r--r-- 1 user user  232019 Aug 24 19:17 commons-beanutils-1.8.3.jar
-rw-r--r-- 1 user user   58160 Aug 24 19:17 commons-codec-1.4.jar
-rw-r--r-- 1 user user  146108 Aug 24 19:17 commons-digester-1.8.1.jar
-rw-r--r-- 1 user user   53082 Aug 24 19:17 commons-fileupload-1.2.jar
-rw-r--r-- 1 user user   87776 Aug 24 19:17 commons-io-1.3.2.jar
-rw-r--r-- 1 user user  284220 Aug 24 19:17 commons-lang-2.6.jar
-rw-r--r-- 1 user user  385091 Aug 24 19:17 commons-lang3-3.2.1.jar
-rw-r--r-- 1 user user   60686 Aug 24 19:17 commons-logging-1.1.1.jar
-rw-r--r-- 1 user user   61829 Aug 22 05:01 commons-logging-1.2.jar
-rw-r--r-- 1 user user      40 Aug 22 05:01 commons-logging-1.2.jar.sha1
-rw-rw-r-- 1 user user   19206 Aug 22 05:01 commons-logging-1.2.pom
-rw-rw-r-- 1 user user      40 Aug 22 05:01 commons-logging-1.2.pom.sha1
-rw-r--r-- 1 user user  390116 Aug 24 19:17 datatables-core-0.9.2.jar
-rw-r--r-- 1 user user    5295 Aug 24 19:17 datatables-export-itext-0.9.2.jar
-rw-r--r-- 1 user user   42220 Aug 24 19:17 datatables-jsp-0.9.2.jar
-rw-r--r-- 1 user user    6484 Aug 24 19:17 datatables-servlet2-0.9.2.jar
-rw-r--r-- 1 user user  313898 Aug 24 19:17 dom4j-1.6.1.jar
-rw-r--r-- 1 user user  750664 Aug 24 19:17 ecore-2.2.3.jar
-rw-r--r-- 1 user user 1346296 Aug 24 19:17 ehcache-core-2.6.6.jar
-rw-r--r-- 1 user user   81271 Aug 24 19:17 hibernate-commons-annotations-4.0.1.Final.jar
-rw-r--r-- 1 user user 4613189 Aug 24 19:17 hibernate-core-4.2.1.Final.jar
-rw-r--r-- 1 user user  138093 Aug 24 19:17 hibernate-ehcache-4.2.1.Final.jar
-rw-r--r-- 1 user user  484520 Aug 24 19:17 hibernate-entitymanager-4.2.1.Final.jar
-rw-r--r-- 1 user user  102661 Aug 24 19:17 hibernate-jpa-2.0-api-1.0.1.Final.jar
-rw-r--r-- 1 user user  477382 Aug 24 19:17 hibernate-validator-4.3.1.Final.jar
-rw-r--r-- 1 user user  352254 Aug 24 19:17 httpclient-4.1.2.jar
-rw-r--r-- 1 user user  433071 Aug 22 02:36 httpclient-4.2.3.jar
-rw-r--r-- 1 user user      40 Aug 22 02:36 httpclient-4.2.3.jar.sha1
-rw-r--r-- 1 user user    5911 Aug 22 02:36 httpclient-4.2.3.pom
-rw-r--r-- 1 user user      40 Aug 22 02:36 httpclient-4.2.3.pom.sha1
-rw-r--r-- 1 user user  181200 Aug 24 19:18 httpcore-4.1.2.jar
-rw-r--r-- 1 user user   38139 Aug 24 19:18 hyperjaxb3-ejb-runtime-0.6.0.jar
-rw-r--r-- 1 user user 1810034 Aug 24 19:18 itextpdf-5.3.4.jar
-rw-r--r-- 1 user user  228268 Aug 24 19:18 jackson-core-asl-1.9.3.jar
-rw-r--r-- 1 user user  773019 Aug 24 19:18 jackson-mapper-asl-1.9.3.jar
-rw-r--r-- 1 user user  648253 Aug 24 19:18 javassist-3.15.0-GA.jar
-rw-r--r-- 1 user user  582024 Aug 24 19:18 javax.mail.jar
-rw-r--r-- 1 user user  141164 Aug 24 19:18 jaxb2-basics-runtime-0.8.2.jar
-rw-r--r-- 1 user user   60768 Aug 24 19:18 jboss-logging-3.1.0.GA.jar
-rw-r--r-- 1 user user   25215 Aug 24 19:18 jboss-transaction-api_1.1_spec-1.0.1.Final.jar
-rw-r--r-- 1 user user   16458 Aug 24 19:18 jcl-over-slf4j-1.7.1.jar
-rw-r--r-- 1 user user  153253 Aug 24 19:18 jdom-1.0.jar
-rw-r--r-- 1 user user  581571 Aug 24 19:18 joda-time-2.3.jar
-rw-r--r-- 1 user user   29691 Aug 24 19:18 joda-time-hibernate-1.3.jar
-rw-r--r-- 1 user user   34417 Aug 24 19:18 joda-time-jsptags-1.1.1.jar
-rw-r--r-- 1 user user  155031 Aug 24 19:18 jquery-2.0.3-1.jar
-rw-r--r-- 1 user user  618763 Aug 24 19:18 jquery-ui-1.10.3.jar
-rw-r--r-- 1 user user   23931 Aug 24 19:18 json-simple-1.1.1.jar
-rw-r--r-- 1 user user   16046 Aug 24 19:18 json-simple-1.1.jar
-rw-r--r-- 1 user user  414240 Aug 24 19:18 jstl-1.2.jar
-rw-r--r-- 1 user user  264600 Aug 24 19:18 logback-classic-1.0.13.jar
-rw-r--r-- 1 user user  418870 Aug 24 19:18 logback-core-1.0.13.jar
-rw-r--r-- 1 user user      98 Aug 22 02:31 m2e-lastUpdated.properties
-rw-r--r-- 1 user user  872300 Aug 24 19:18 mysql-connector-java-5.1.27.jar
-rw-rw-r-- 1 user user     199 Aug 22 05:01 _remote.repositories
-rw-r--r-- 1 user user  219683 Aug 24 19:18 rome-1.0.jar
-rw-r--r-- 1 user user   26084 Aug 24 19:18 slf4j-api-1.7.5.jar
-rw-r--r-- 1 user user  335679 Aug 24 19:18 spring-aop-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  359456 Aug 24 19:18 spring-aop-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  610657 Aug 24 19:18 spring-beans-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  712395 Aug 24 19:18 spring-beans-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  865109 Aug 24 19:18 spring-context-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user 1026661 Aug 24 19:18 spring-context-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  127744 Aug 24 19:18 spring-context-support-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  178151 Aug 24 19:18 spring-context-support-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  870616 Aug 24 19:18 spring-core-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user 1008584 Aug 24 19:18 spring-core-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  382147 Aug 24 19:18 spring-data-commons-1.6.2.RELEASE.jar
-rw-r--r-- 1 user user  161900 Aug 24 19:18 spring-data-jpa-1.4.2.RELEASE.jar
-rw-r--r-- 1 user user  196929 Aug 24 19:18 spring-expression-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  259197 Aug 24 19:18 spring-expression-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  405160 Aug 24 19:18 spring-jdbc-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  426716 Aug 24 19:18 spring-jdbc-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  205995 Aug 24 19:18 spring-jms-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  269447 Aug 24 19:18 spring-jms-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  285829 Aug 24 19:18 spring-messaging-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  385012 Aug 24 19:18 spring-orm-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  376254 Aug 24 19:18 spring-orm-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user   74705 Aug 24 19:18 spring-oxm-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user   82949 Aug 24 19:18 spring-oxm-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user   83411 Aug 24 19:18 spring-security-acl-3.2.2.RELEASE.jar
-rw-r--r-- 1 user user  406230 Aug 24 19:18 spring-security-config-3.2.2.RELEASE.jar
-rw-r--r-- 1 user user  359632 Aug 24 19:18 spring-security-core-3.2.2.RELEASE.jar
-rw-r--r-- 1 user user   24511 Aug 24 19:18 spring-security-taglibs-3.2.2.RELEASE.jar
-rw-r--r-- 1 user user  342221 Aug 24 19:18 spring-security-web-3.2.2.RELEASE.jar
-rw-r--r-- 1 user user  242189 Aug 24 19:18 spring-tx-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  252964 Aug 24 19:18 spring-tx-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  627339 Aug 24 19:18 spring-web-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  722215 Aug 24 19:18 spring-web-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user  637193 Aug 24 19:18 spring-webmvc-3.2.5.RELEASE.jar
-rw-r--r-- 1 user user  782723 Aug 24 19:18 spring-webmvc-4.1.7.RELEASE.jar
-rw-r--r-- 1 user user   26664 Aug 24 19:18 tiles-api-2.1.3.jar
-rw-r--r-- 1 user user  135193 Aug 24 19:18 tiles-core-2.1.3.jar
-rw-r--r-- 1 user user   55083 Aug 24 19:18 tiles-jsp-2.1.3.jar
-rw-r--r-- 1 user user   53036 Aug 24 19:18 tiles-servlet-2.1.3.jar
-rw-r--r-- 1 user user  124006 Aug 24 19:18 tomcat-jdbc-7.0.42.jar
-rw-r--r-- 1 user user   38333 Aug 24 19:18 tomcat-juli-7.0.42.jar
-rw-r--r-- 1 user user  165092 Aug 24 19:18 twilio-java-sdk-3.4.5.jar
-rw-r--r-- 1 user user      40 Aug 22 02:31 twilio-java-sdk-3.4.5.jar.sha1
-rw-r--r-- 1 user user    5113 Aug 22 02:31 twilio-java-sdk-3.4.5.pom
-rw-r--r-- 1 user user      40 Aug 22 02:31 twilio-java-sdk-3.4.5.pom.sha1
-rw-r--r-- 1 user user  129098 Aug 22 02:31 twilio-java-sdk-3.4.5-sources.jar
-rw-r--r-- 1 user user      40 Aug 22 02:31 twilio-java-sdk-3.4.5-sources.jar.sha1
-rw-r--r-- 1 user user  134530 Aug 24 19:18 usertype.core-3.1.0.CR8.jar
-rw-r--r-- 1 user user   53688 Aug 24 19:18 usertype.spi-3.1.0.CR8.jar
-rw-r--r-- 1 user user   47433 Aug 24 19:18 validation-api-1.0.0.GA.jar

然后,我检查ls -al了依赖项上面的文件夹的内容是否有任何内容,但是得到了以下结果,似乎表明它们为空:

[[email protected] dependencies]$ cd /home/user/javacode/dependencies/1.2/
[[email protected] 1.2]$ ls -al
total 12
drwxrwxr-x 2 user user    6 Aug 22 05:05 .
drwxrwxr-x 5 user user 8192 Aug 24 19:18 ..
[[email protected] 1.2]$ cd /home/user/javacode/dependencies/3.4.5/
[[email protected] 3.4.5]$ ls -al
total 12
drwxr-xr-x 2 user user    6 Aug 22 03:00 .
drwxrwxr-x 5 user user 8192 Aug 24 19:18 ..
[[email protected] 3.4.5]$ cd /home/user/javacode/dependencies/4.2.3/
[[email protected] 4.2.3]$ ls -al
total 12
drwxr-xr-x 2 user user    6 Aug 22 03:01 .
drwxrwxr-x 5 user user 8192 Aug 24 19:18 ..
[[email protected] 4.2.3]$ 

那么,如何解决此错误并使该程序从命令行成功运行?


回答:

解决方法是在终端上导航到dependencies目录,然后键入以下四个命令,然后再尝试从命令行运行Java代码:

rm httpclient-4.2.3.pom
rm httpclient-4.2.3.pom.sha1
rm httpclient-4.2.3.jar
rm httpclient-4.2.3.jar.sha1

用户在下面的大致方向上暗示了一个猜测,因此我将其标记为可接受的答案。但是实际的解决方案是四行rm命令,从而消除了冗余httpclient库。

约翰·布林格

如果NoSuchFieldError在运行时得到一个,最可能的解释是,运行时提供的至少一个类的版本与编译时提供的版本不同,并且您尝试使用的类引用了编译时版本的字段在同一类的运行时版本中不存在的已更改类的名称。

很难确切判断不兼容的原因,但我的第一个猜测是httpclient和httpcore的不兼容版本。请注意,尽管您只有后者的一个版本,但您有前者的两个版本。我想,很有可能版本号完全匹配的httpclient和httpcore是一起使用的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

来自分类Java

java.lang.NoSuchFieldError:INSTANCE

来自分类Java

java.lang.NoSuchFieldError:默认

来自分类Dev

java.lang.NoSuchFieldError:反思

来自分类Java

带有反射的java.lang.SecurityException

来自分类Java

带有弹性搜索的Spring Boot导致java.lang.NoSuchFieldError:IGNORE_DEPRECATIONS

来自分类Dev

NullnessChecker错误:java.lang.NoSuchFieldError:释放

来自分类Dev

更改类后java.lang.NoSuchFieldError

来自分类Dev

ScalaFx MouseEvent:java.lang.NoSuchFieldError:返回

来自分类Java

java.lang.OutOfMemoryError:带有蜂巢的Java堆空间

来自分类Java

Linux中的java.lang.UnsatisfiedLinkError

来自分类Java

java.lang.NoClassDefFoundError:java.nio.charset.StandardCharsets

来自分类Dev

带有ViewPager的Android java.lang.IllegalStateException

来自分类Dev

带有大量图像的android java.lang.OutOfMemoryError recyclerview

来自分类Java

带有null消息和原因的java.lang.ClassCastException

来自分类Java

带有导入库的Android Studio java.lang.NoSuchMethodError

来自分类Dev

带有KitKat的Android设备上的java.lang.NoClassDefFoundError

来自分类Dev

java.lang.IllegalArgumentException:带有改型的非法URL

来自分类Java

春季:java.lang.NoSuchFieldError:IMPORT_BEAN_NAME_GENERATOR

来自分类Java

java.lang.NoSuchFieldError的:ACCEPT_CASE_INSENSITIVE_VALUES

来自分类Java

“java.lang.NoSuchFieldError的:超级”异常 - 错误的编译器?

来自分类Dev

使用ScalaTest时出现java.lang.NoSuchFieldError

来自分类Java

线程“主”中的异常java.lang.NoSuchFieldError:TRACE

来自分类Java

Spring 3.1 entityManagerFactory java.lang.NoSuchFieldError:NULL错误

来自分类Dev

JSON解析器-java.lang.NoSuchFieldError:defaultReader

来自分类Dev

Eclipse无法启动:java.lang.NoSuchFieldError:url

来自分类Dev

Xamarin Android java.lang.InstantiationException:java.lang.Class没有零参数构造函数

来自分类Java

java.lang.Math和java.lang.StrictMath有什么区别?

来自分类Java

java.lang.ClassNotFoundException:来自BaseClassLoader的org.apache.commons.lang.StringUtils

来自分类Dev

Android SDK 26及更高版本-自定义数组对象的JNI GetObjectField调用带有待处理异常java.lang.NoSuchFieldError

TOP 榜单

热门标签

归档