java.lang.NoSuchFieldError:使用twilio从linuxterminal获取DEF_CONTENT_CHARSET

在从CentOSterminal运行的一个小型java程序中,当我inputjava -cp .:"../dependencies/*" mainpackage.SendText xxxxxxxxxx HelloThere时,出现java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET错误java -cp .:"../dependencies/*" mainpackage.SendText xxxxxxxxxx HelloThere 7terminal的java -cp .:"../dependencies/*" mainpackage.SendText xxxxxxxxxx HelloThere如何解决此错误,以便程序可以从命令行成功运行?

terminalinput输出如下:

 [user@domain 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 

我读了另外 httpcore 文章,指出这个问题是由classpathhttpcore冗余版本引起的,所以我把它移到了父目录下,并且对它们名字中的所有带有httpcore文件进行了recursionsearch,但得到了如下结果只显示一个版本的httpcore

 [user@domain bin]$ cd /home/user/javacode/ [user@domain 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 [user@domain javacode]$ 

TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);错误的代码行是: 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文件夹的全部内容是:

 [user@domain bin]$ cd /home/user/javacode/dependencies [user@domain 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依赖关系中文件夹的内容是否有任何内容,但得到了下面的结果,这似乎表明它们是空的:

 [user@domain dependencies]$ cd /home/user/javacode/dependencies/1.2/ [user@domain 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 .. [user@domain 1.2]$ cd /home/user/javacode/dependencies/3.4.5/ [user@domain 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 .. [user@domain 3.4.5]$ cd /home/user/javacode/dependencies/4.2.3/ [user@domain 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 .. [user@domain 4.2.3]$ 

那么如何解决这个错误并让程序从命令行成功运行?


回答:

解决方法是将terminal导航到dependencies目录,然后在尝试从命令行再次运行java代码之前,input以下四个命令:

 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 

一个用户在下面的这个大方向猜测,所以我把它标记为接受的答案。 但实际的解决scheme是rm命令的四行,这消除了多余的httpclient库。

如果在运行时得到一个NoSuchFieldError ,最可能的解释就是在运行时出现至少一个类的不同版本,而不是在编译时出现的, 您尝试使用的类引用编译时的字段版本更改的类不存在于同一类的运行时版本中。

很难判断不兼容性在哪里,但我的第一个猜测是不兼容的httpclient和httpcore版本。 请注意,虽然你只有后者的一个版本,但你有两个版本的前者。 我想最有可能的是,具有完全匹配的版本号的httpclient和httpcore是一起工作的。