简体   繁体   中英

In Eclipse, why am I getting the error “Error: Could not find or load main class Coordinator.java”?

I have a project to run a matrix-multiplication demo via Java networking.

I am getting the following error when I run Eclipse :

'Error: Could not find or load main class Coordinator.java'

But the class I am running does have a main method. I'm not sure what is going on with this.

I include screenshot here of the Eclipse screen where I was : http://i.imgur.com/uMoUcmp.png

I realize it's most likely an eclipse/configuration error ,but anyway I include here below the Worker.java and DataIO.java code , altogether it is 5 classes, and the other 3 I put into a jsfiddle , though its not Java ( MatrixMultiple.java Connection.java Coordinator.java DataIO.java, and Worker.java - Coordinator.java is which runs the java code ; )

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 with the rest of Java code in it = Coordinator.java ; MatrixMultiple.java ; Connection.java :

http://jsfiddle.net/31y0ehep/

thanks so much

I can't really tell why you also have a main method in your Worker class, but to make sure it isn't an Eclipse related error try the following:

  1. Right click your project;

  2. At Run As, go to Run Configurations;

  3. Delete every run configuration related to your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM