我有一个项目通过Java网络运行矩阵乘法演示。
运行Eclipse时出现以下错误:
'Error: Could not find or load main class Coordinator.java'
但我正在运行的类有一个main方法。我不确定这是怎么回事。
我在这里包含Eclipse屏幕的截图:http://i.imgur.com/uMoUcmp.png
我意识到它很可能是一个eclipse /配置错误,但无论如何我在这里包含了Worker.java和DataIO.java代码,总共它是5个类,而另外3个我放入了一个jsfiddle,尽管它不是Java (MatrixMultiple.java Connection.java Coordinator.java DataIO.java和Worker.java - Coordinator.java运行java代码;)
package socket_example_1row;
import java.io.*;
import java.util.Scanner;
import java.net.InetAddress;
import java.util.Arrays;
import matrix.*;
public class Worker {
int nodeNum;
int localPort;
Connection conn;
int dim;
int width;
int[][] a;
int[][] b;
int[][] c;
DataInputStream disCoor;
DataOutputStream dosCoor;
DataOutputStream dosLeft;
DataInputStream disRight;
public Worker(int nodeNum, int localPort) {
this.nodeNum = nodeNum;
this.localPort = localPort;
}
void configurate(String coorIP, int coorPort) {
try {
conn = new Connection(localPort);
DataIO dio = conn.connectIO(coorIP, coorPort);
dosCoor = dio.getDos();
dosCoor.writeInt(nodeNum);
dosCoor.writeUTF(InetAddress.getLocalHost().getHostAddress());
dosCoor.writeInt(localPort);
disCoor = dio.getDis();
dim = disCoor.readInt(); //get matrix dimension from coordinator
width = disCoor.readInt();
a = new int[dim][width];
b = new int[dim][width];
c = new int[dim][width];
String ipLeft = disCoor.readUTF(); //left block connection info
int portLeft = disCoor.readInt();
String ipRight = disCoor.readUTF(); //right block connection info
int portRight = disCoor.readInt();
if (nodeNum%2==0) { // Even # worker connecting manner
dosLeft = conn.connect2write(ipLeft, portLeft);
disRight = conn.accept2read();
} else { // Odd # worker connecting manner
disRight = conn.accept2read();
dosLeft = conn.connect2write(ipRight, portRight);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Configuration done.");
}
// shift matrix a toward left, even # worker send before receive
void shiftLeftSend1st(int[][] mat, DataOutputStream dosL, DataInputStream disR) {
// send leftmost column
for (int i = 0; i < dim; i++) {
try {
dosLeft.writeInt(mat[i][0]);
} catch (IOException ioe) {
System.out.println("error in sending to left, row=" + i);
ioe.printStackTrace();
}
}
// local shift
for (int i = 0; i < dim; i++) {
for (int j = 1; j < width; j++) {
mat[i][j - 1] = mat[i][j];
}
}
// receive the rightmost column
for (int i = 0; i < dim; i++) {
try {
mat[i][width - 1] = disRight.readInt();
} catch (IOException ioe) {
System.out.println("error in receiving from right, row=" + i);
ioe.printStackTrace();
}
}
}
// shift matrix a toward left, odd # worker receive before send
void shiftLeftReceive1st(int[][] mat, DataOutputStream dosL, DataInputStream disR) {
int[] tempIn = new int[dim];
int[] tempOut = new int[dim];
for (int i = 0; i < dim; i++) { // receive a column from right
try {
tempIn[i] = disRight.readInt();
} catch (IOException ioe) {
System.out.println("error in receiving from right, row=" + i);
ioe.printStackTrace();
}
}
for (int i = 0; i < dim; i++) { // local shift
tempOut[i] = a[i][0];
}
for (int i = 0; i < dim; i++) {
for (int j = 1; j < width; j++) {
a[i][j - 1] = a[i][j];
}
}
for (int i = 0; i < dim; i++) {
a[i][width - 1] = tempIn[i];
}
for (int i = 0; i < dim; i++) { // send leftmost column to left node
try {
dosLeft.writeInt(tempOut[i]);
} catch (IOException ioe) {
System.out.println("error in sending left, row=" + i);
ioe.printStackTrace();
}
}
}
// shift matrix upward
void shiftUp(int[][] mat) {
// copy top row
int[] tempTop = new int[mat[0].length];
for (int j = 0; j < tempTop.length; j++) {
tempTop[j] = mat[0][j];
}
// local shift
for (int i = 0; i < mat.length-1; i++) {
for (int j = 0; j < mat[0].length; j++) {
mat[i][j] = mat[i+1][j];
}
}
for (int j = 0; j < tempTop.length; j++) {
mat[mat.length-1][j] = tempTop[j];
}
}
void compute() {
// get the block of a from coordinator
for (int i = 0; i < dim; i++) {
for (int j = 0; j <width; j++) {
try {
a[i][j] = disCoor.readInt();
} catch (IOException ioe) {
System.out.println("error: matrix a " + i + ", " + j);
ioe.printStackTrace();
}
}
}
MatrixMultiple.displayMatrix(a);
// get the block of b from coordinator
for (int i = 0; i < dim; i++) {
for (int j = 0; j <width; j++) {
try {
b[i][j] = disCoor.readInt();
} catch (IOException ioe) {
System.out.println("error: matrix b " + i + ", " + j);
ioe.printStackTrace();
}
}
}
MatrixMultiple.displayMatrix(b);
c = new int[a.length][a[0].length];
for (int i=0; i < a.length; i++) {
Arrays.fill(c[i], 0);
}
// multiplication
for (int r = 0; r < a.length; r++) {
// computer one term in c
for (int i = 0; i < a.length; i++) {
for (int j = 0; j <a[0].length; j++) {
c[i][j] = c[i][j] + a[i][j] * b[i][j];
}
}
System.out.println("Matrix c");
MatrixMultiple.displayMatrix(c, 8);
// shift matrix a toward left
if (nodeNum%2==0) { // Even # worker shifting procedure
shiftLeftSend1st(a, dosLeft, disRight);
} else { // Odd # worker shifting procedure
shiftLeftReceive1st(a, dosLeft, disRight);
}
shiftUp(b);
System.out.println("Shifted matrix a, r=" + r);
MatrixMultiple.displayMatrix(a);
System.out.println("Shifted matrix b");
MatrixMultiple.displayMatrix(b);
}
System.out.println("Matrix c");
MatrixMultiple.displayMatrix(c, 8);
}
public static void main(String[] args) {
if (args.length != 4) {
System.out.println("usage: java Worker workerID worker-port-num coordinator-ip coordinator-port-num");
}
int workerID = Integer.parseInt(args[0]);
int portNum = Integer.parseInt(args[1]);
Worker worker = new Worker(workerID, portNum);
worker.configurate(args[2], Integer.parseInt(args[3]));
worker.compute();
try {
Thread.sleep(6000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Press the enter key to exit.");
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException ioe) {ioe.printStackTrace();}
}
}
DataIO.java
package socket_example_1row;
import java.io.*;
public class DataIO {
DataInputStream dis;
DataOutputStream dos;
public DataIO(DataInputStream dis, DataOutputStream dos) {
this.dis = dis;
this.dos = dos;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
}
jsfiddle与其余的Java代码= Coordinator.java; MatrixMultiple.java; Connection.java:
非常感谢
答案 0 :(得分:0)
我无法解释为什么你的Worker类中也有一个main方法,但为了确保它不是Eclipse相关的错误,请尝试以下方法:
右键单击您的项目;
在“运行方式”中,转到“运行配置”;
删除与项目相关的每个运行配置。