所以我让我的项目有效。我通过套接字连接发送对象,所以我不得不在我的客户端(android项目)添加一个类。现在我得到了这个例外:
java.lang.ClassNotFoundException: [LDB.LuceneSearchEngine$LuceneSearchResults;
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:324)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2258)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
at java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1418)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:759)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
at com.shoppiness.radu.jshoppiness.Client.run(Client.java:82)
at com.shoppiness.radu.jshoppiness.MainActivity$connectTask.doInBackground(MainActivity.java:77)
at com.shoppiness.radu.jshoppiness.MainActivity$connectTask.doInBackground(MainActivity.java:61)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "DB.LuceneSearchEngine$LuceneSearchResults" on path: DexPathList[[zip file "/data/app/com.shoppiness.radu.jshoppiness-1/base.apk"],nativeLibraryDirectories=[/data/app/com.shoppiness.radu.jshoppiness-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:324)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2258)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
at java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1418)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:759)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
at com.shoppiness.radu.jshoppiness.Client.run(Client.java:82)
at com.shoppiness.radu.jshoppiness.MainActivity$connectTask.doInBackground(MainActivity.java:77)
at com.shoppiness.radu.jshoppiness.MainActivity$connectTask.doInBackground(MainActivity.java:61)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Suppressed: java.lang.ClassNotFoundException: DB.LuceneSearchEngine$LuceneSearchResults
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 19 more
Caused by: `java.lang.NoClassDefFoundError`: Class is not found using the boot class loader; no stack trace available
LuceneSearchEngine.java
是我刚刚添加的课程。在LuceneSearchEngine
中声明了另一个类LuceneSearchResults
。
我尝试删除这些类,然后使用稍微不同的名称再次创建它们。当我运行时,即使从项目中删除该类,我也会得到该异常。
奇怪的是我创建了一个新项目并粘贴了旧类。然后我添加了LuceneSearchEngine
和LuceneSearchEngineResults
,但命名方式不同。即使这些类从未添加到新项目中,我也得到完全相同的异常。
我尝试过清洁和重建,但它不起作用。
清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
package com.shoppiness.radu.jshoppiness;
import java.io.File;
// TODO: create final strings for hardcodede stuff...
public class LuceneSearchEngine {
public static class LuceneSearchResults
{
private static String userSearch = "";
public String getUserSearch() {
return userSearch;
}
public static void setUserSearch(String userSearch1) {
userSearch = userSearch1;
}
private String product_code;
private String adv_name;
private String category;
private String manufacturer;
private String gift_included;
private String product_name;
private String product_desc;
private String product_aff_link;
private String product_pic;
private String price_no_vat;
private String price_vat;
private String free_shipping;
public LuceneSearchResults()
{
product_code = null;
adv_name = "";
category = null;
}
private static int productScore(LuceneSearchResults result)
{
int score = 0;
if(LuceneSearchResults.userSearch.contains(result.product_code))
return 666;
else
{
if(LuceneSearchResults.userSearch.contains(result.adv_name))
score+=4;
if(LuceneSearchResults.userSearch.contains(result.manufacturer))
score+=3;
if(LuceneSearchResults.userSearch.contains(result.category))
score+=2;
if(LuceneSearchResults.userSearch.contains(result.product_name))
score++;
return score;
}
}
// quicksort
private static int partition(LuceneSearchResults results[], int left, int right)
{
int i = left, j = right;
LuceneSearchResults tmp;
int pivot = productScore(results[(left + right) / 2]);
while (i <= j) {
while (productScore(results[i]) < pivot)
i++;
while (productScore(results[j]) > pivot)
j--;
if (i <= j) {
tmp = results[i];
results[i] = results[j];
results[j] = tmp;
i++;
j--;
}
};
return i;
}
public static void quickSort(LuceneSearchResults results[], int left, int right) {
int index = partition(results, left, right);
if (left < index - 1)
quickSort(results, left, index - 1);
if (index < right)
quickSort(results, index, right);
}
//
// getters and setters
public String getProduct_code() {
return product_code;
}
public void setProduct_code(String product_code) {
this.product_code = product_code;
}
public String getAdv_name() {
return adv_name;
}
public void setAdv_name(String adv_name) {
this.adv_name = adv_name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getGift_included() {
return gift_included;
}
public void setGift_included(String string) {
this.gift_included = string;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getProduct_desc() {
return product_desc;
}
public void setProduct_desc(String product_desc) {
this.product_desc = product_desc;
}
public String getProduct_aff_link() {
return product_aff_link;
}
public void setProduct_aff_link(String product_aff_link) {
this.product_aff_link = product_aff_link;
}
public String getProduct_pic() {
return product_pic;
}
public void setProduct_pic(String product_pic) {
this.product_pic = product_pic;
}
public String getPrice_no_vat() {
return price_no_vat;
}
public void setPrice_no_vat(String string) {
this.price_no_vat = string;
}
public String getPrice_vat() {
return price_vat;
}
public void setPrice_vat(String string) {
this.price_vat = string;
}
public String getFree_shipping() {
return free_shipping;
}
public void setFree_shipping(String string) {
this.free_shipping = string;
}
}
public static final File INDEX_DIRECTORY = new File("IndexDirectory");
public int getHitsCount()
{
return -1;
}
public void createIndex() {
}
public LuceneSearchResults[] search() {
return null;
}
public static void main(String[] args) {
}
}
Client.java // here
package com.shoppiness.radu.shoppiness;
import android.util.Log;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
private LuceneEngine.LuceneSearchResults[] serverMessage;
public static String SERVERIP = "5.15.110.111" ; // your computer IP
// address
public static final int SERVERPORT = 3316;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
PrintWriter out;
ObjectInputStream in;
/**
* Constructor of the class. OnMessagedReceived listens for the messages
* received from server
*/
public Client(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
*
* @param message
* text entered by client
*/
public void sendMessage(String message) {
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}
public void stopClient() {
mRun = false;
}
public void run() {
mRun = true;
try {
// here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("serverAddr", serverAddr.toString());
Log.e("TCP Client", "C: Connecting...");
// create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
Log.e("TCP Server IP", SERVERIP);
try {
// send the message to the server
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
// receive the message which the server sends back
in = new ObjectInputStream(socket.getInputStream());
// in this while the client listens for the messages sent by the
// server
while (mRun) {
serverMessage = new LuceneEngine.LuceneSearchResults[1000];
serverMessage = (LuceneEngine.LuceneSearchResults[]) in.readObject();
if (serverMessage != null && mMessageListener != null) {
// call the method messageReceived from MyActivity class
Log.d("Radu:", "Object recieved!");
mMessageListener.messageReceived(serverMessage[0].getAdv_name());
Log.d("WIN::", serverMessage[0].getAdv_name());
}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '"
+ serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
// the socket must be closed. It is not possible to reconnect to
// this socket
// after it is closed, which means a new socket instance has to
// be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
// Declare the interface. The method messageReceived(String message) will
// must be implemented in the MyActivity
// class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(Object message);
}
}
connectTask.java
public class connectTask extends AsyncTask<String,String,Client> {
@Override
protected Client doInBackground(String... message) {
//we create a Client object and
Log.d("Radu:", "new client");
mClient = new Client(new Client.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(Object message) {
//this method calls the onProgressUpdate
Log.d("Radu:", "publishProgress...");
publishProgress(message.toString());
}
});
mClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
Log.e("OBJECT_RECIEVED:", values[0].toString());
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
}
}
答案 0 :(得分:1)
日志非常明确:
java.lang.ClassNotFoundException: DB.LuceneSearchEngine$LuceneSearchResults
您正在尝试从包LuceneSearchEngine$LuceneSearchResults
加载课程DB
。
在发布的代码中,该类的包是com.shoppiness.radu.jshoppiness
。
同样从日志开始,错误从com.shoppiness.radu.jshoppiness.MainActivity$connectTask.doInBackground(MainActivity.java:61)
开始,因此最好在此处查找错误。
<强>更新强>
你发送&#34;错误&#34;对象,当您在以下行中读取它时,您将获得类强制转换异常:
serverMessage = (LuceneEngine.LuceneSearchResults[]) in.readObject();
问题是班级的包。