【问题标题】:"can't find main class" error in terminal but not in eclipse终端中的“找不到主类”错误,但在 Eclipse 中没有
【发布时间】:2013-06-20 09:26:51
【问题描述】:

这个程序获取现实生活中的物体及其权重,并根据字母顺序然后按数字对其进行排序

但问题是当我在 Eclipse 中运行这个应用程序时(还没有在 NetBeans 中尝试过),它运行良好,但是当我编译并尝试在终端中运行它时,它不起作用并出现错误, “找不到主类”

记住我的java文件和class文件在同一个文件夹,所以目录不是问题

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());
        }
    }
}

}

根据要求: 在我输入的命令行中,

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

【问题讨论】:

标签: java class main


【解决方案1】:

要在终端中编译/运行 Java 程序,请执行以下操作:

  1. 转到您的程序所在的目录(您可以使用cd“更改目录”命令这样做)。
    • 在 Windows 上,要访问您的桌面,应为:cd C:\Users\YourLogin\Desktop
    • 在 Mac 上为:cd ~/Users/YourLogin/Desktop
  2. 要编译,输入javac NameOfProgram.java,例如javac testMain.java
  3. 要运行,请输入java NameOfProgram,例如java testMain

【讨论】:

  • +1 非常好的答案。重申一下,您为步骤 3 键入“.class”扩展名。您指定 Java 类名,而不是类的完整文件名。
  • @MonadNewb 是的,我在看到答案后才意识到这一点。我现在觉得自己像个白痴。
  • @user2034125,没关系。我们都在某个时候在那里。祝你好运!
猜你喜欢
  • 2013-02-09
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2014-03-28
  • 2014-11-18
  • 2015-07-25
  • 1970-01-01
相关资源
最近更新 更多