[英]NoClassDefError when using classes from a .jar
我試圖在我的.java文件Tweetauthent中使用.jar文件中的類。 .jar文件在另一個目錄中。 我向Twitter REST API發出請求以獲取Bearertoken。 TweetAuthent在我運行時編譯
-javac -cp /path/to/jar Tweetauthent.java
這是代碼
import java.io.BufferedReader;
import java.lang.StringBuilder;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
public class Tweetauthent{
static String consumerkey = "astring";
static String consumersecret = "astring";
static String endurl = "https://api.twitter.com/oauth2/token";
public static void main(String []args){
Tweetauthent t = new Tweetauthent();
try{
System.out.println(t.requestBearerToken(endurl));
}catch(IOException e){
System.out.println(e);
}
}
public static String encodeKeys(String consumerKey,String consumerSecret){
try{
String encodedConsumerKey = URLEncoder.encode(consumerKey,"UTF-8");
String encodedConsumerSecret = URLEncoder.encode(consumerSecret,"UTF-8");
String fullKey = encodedConsumerKey + ":" + encodedConsumerSecret;
byte[] encodedBytes = Base64.getEncoder().encode(fullKey.getBytes());
return new String(encodedBytes);
}catch(UnsupportedEncodingException e){
return new String();
}
}
public static String requestBearerToken(String endPointURL) throws IOException {
HttpsURLConnection connection = null;
String encodedCredentials = encodeKeys("<consumerkey>","<consumersecret>");
try{
URL url = new URL(endPointURL);
connection = (HttpsURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Host","api.twitter.com");
connection.setRequestProperty("User-Agent","TweetPersonalityAnalyzer");
connection.setRequestProperty("Authorization","Basic " + encodedCredentials);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length","29");
writeRequest(connection, "grant_type=client_credentials");
JSONObject obj = (JSONObject)JSONValue.parse(readResponse(connection));
if(obj != null){
String tokenType = (String)obj.get("token_type");
String token = (String)obj.get("access_token");
return ((tokenType.equals("bearer")) && (token != null)) ? token : "";
}
return new String();
}catch(MalformedURLException e){
throw new IOException("Invalid endpoint URL specified.", e);
}
finally{
if( connection != null){
connection.disconnect();
}
}
}
public static boolean writeRequest(HttpsURLConnection connection, String textBody){
try{
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
wr.write(textBody);
wr.flush();
wr.close();
return true;
}
catch(IOException e){
return false;
}
}
public static String readResponse(HttpsURLConnection connection){
try {
StringBuilder str = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
while((line = br.readLine()) != null){
str.append(line + System.getProperty("line.separator"));
}
return str.toString();
}catch(IOException e){
return new String();
}
}
我運行時遇到的錯誤
java Tweetauthent
是
java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at Tweetauthent.requestBearerToken(Tweetauthent.java:61)
at Tweetauthent.main(Tweetauthent.java:27)
Caused by: java.lang.ClassNotFoundException:
org.json.simple.JSONValue
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
據我了解,當JVM無法找到.java文件用於編譯的必要的類文件時,會引發NoClassDefFoundError。 是什么原因造成的? 有沒有辦法添加jar的路徑-java? 哦,還有預期的consumerkey和秘密字符串。
更新:當我向Java添加類路徑時
java /path/to/jar Tweethauthent
我得到錯誤:找不到或加載主類Tweetauthent
任何幫助將不勝感激,謝謝!
您需要在java
命令的-classpath
或-cp
選項中引用運行時依賴項:
java -classpath /path/to/jar Tweetauthent
如果要使用參數運行,請將其作為參數添加到Java文件之后:
java -classpath /path/to/jar Tweetauthent <consumerkey> <consumersecret>
您遇到運行時類路徑問題。
我看不到您如何設置運行時類路徑。 您編譯的事實是必要的,但不足。
您可能沒有正確設置運行時類路徑,或者JAR不屬於您的軟件包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.