Search code examples
javaclassprogram-entry-point

"can't find main class" error in terminal but not in eclipse


This program take real life objects and its weights and sorts it based on alphebatical order and then number-wise

But the problem is that when I run this app in Eclipse (haven't tried in NetBeans), it runs fine, but when I compile and try to run it in terminal, it doesn't work and comes with error, "could not find main class"

Keep in mind my java file and class file are in the same folder, so the directory is not the problem

public class testMain {

public static void main(String[] args) {
    //makes a new multidimensial array
    //the first dimension holds the name of the object 
    //the second dimension holds the weight
    //the 4's in this case show the maximum space the array can hold
    String[][] objectList = new String[4][4];

    objectList[1][0] = "Teapot";
    objectList[0][1] = String.valueOf(1);

    objectList[2][0] = "Chesterfield";
    objectList[2][2] = String.valueOf(120);

    objectList[3][0] = "Laptop";
    objectList[3][3] = String.valueOf(6);

    //printing the array
    for (int i = 1; i < 3; i++) {
        for (int j = 0; j < 3;) {
            System.out.println(objectList[i][j].toString());
        }
    }
}

}

By request: In the command line I put,

cd /Users/username/Desktop/JavaProjects
javac ojArray.java
(After it compiled)
java ojArray.class

Solution

  • To compile/run a Java program in terminal, you do the following:

    1. Go to the directory that your program is in (you can use the cd "change directory" command to do so).
      • On Windows, to get to your desktop, it would be: cd C:\Users\YourLogin\Desktop
      • On Mac, it would be: cd ~/Users/YourLogin/Desktop
    2. To compile, type javac NameOfProgram.java, eg javac testMain.java
    3. To run, type java NameOfProgram, eg java testMain