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

依然范特西╮ 提交于 2020-01-15 07:07:19

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/17265883/cant-find-main-class-error-in-terminal-but-not-in-eclipse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!