运行编译的Java类

时间:2012-03-10 16:15:25

标签: java

我编译了两个java源代码,即version.class和open.class

基本上,version.class包含public static void main子句,而open.class主要是一组使整个程序工作的构造函数。

我是这样做的:

if ("n".equalsIgnoreCase(input)) {
    new open();
} else if ("q".equalsIgnoreCase(input)) {
        System.exit(0);
    }

问题是当我在CMD中运行它时,我输入了它:

java version

它表示该命令无法找到或加载主类。发生这种情况的主要问题是什么?谢谢。我希望在编译时也可以在CMD中运行,所以我可以创建一个bat文件来使程序在CMD中运行。 编辑:

这是整个版本类:

import java.util.Scanner;
public class version{
public static String input;
public static Scanner s = new Scanner (System.in);

public static void main (String [] args ) {
    System.out.println("========================");
    System.out.println("ANAGRAM MASTER VERSION 1.0");
    System.out.println("Created by: Janrae Mendoza");
    System.out.println("==========================");
    System.out.println("Press the corresponding letters for your desired option: ");
    System.out.println("<n> New Game");
    System.out.println("<q> Quit Game");
    input = s.nextLine();

if ("n".equalsIgnoreCase(input)) {
    new open();
} else if ("q".equalsIgnoreCase(input)) {
        System.exit(0);
    }
    }

}

虽然这是公开课:

import java.util.Random;
import java.util.Scanner;


public class open{

public String input;
public Scanner s = new Scanner (System.in);
public Random r = new Random();

//at least 25 words
public String scrambled[] = {"MICSECO", "LEPAIM", "ICHORE", "MLORTA", "TIEEXSUQI",
        "MEERTO", "DESTOAK", "MLIBOE", "PHRAPARGA", "UALBILNGI", "DOXORTHRO",
        "ZEIMESREM", "OKVEPRO", "ILAPRS", "TEMOED", "QUEMSADRAE", "LSEIMUP",
        "CREUPRO", "BLRUNDE", "CKRENO", "UIPRSUT", "BINMEHLOGO", "DRKERA",
        "SEACEDAP", "SOULEAZ"};

public String scrAns[] = {"ECONOMICS", "IMPALE", "HEROIC", "MORTAL", "EXQUISITE",
        "REMOTE", "STOCKADE", "MOBILE", "PARAGRAPH", "BILINGUAL", "ORTHRODOX",
        "MESMERIZE", "PROVOKE", "SPIRAL", "DEMOTE", "MASQUERADE", "IMPULSE",
        "PROCIRE", "BLUNDER", "RECKON", "PURSUIT", "HEMOGLOBIN", "DARKER",
        "ESCAPADE", "ZEALOUS"};

public int word;
public boolean stop = false;
public int scrnum;
public int timesplayed;

public void sleep() {
    try {
        Thread.sleep(1000);
    } catch (Exception e) {}
}

public open() {
        while (stop == false) {
            sleep();
    System.out.println("Guess out the scrambled word!");
    word = r.nextInt(24);
    scrnum = word;
    System.out.println("ANAGRAM: " + scrambled[word]);
    System.out.print("YOUR ANSWER: ");
    input = s.nextLine();
        if (input.equalsIgnoreCase(scrAns[scrnum])) {
            System.out.println("You guessed the word right!");
            System.out.println("Continue playing? <y> Yes and any other keys for No");
            input = s.nextLine();
                if ("y".equalsIgnoreCase(input)) {
                    timesplayed++;
                } else {
                                        stop = true;
                                    }
        }

        }
        System.out.println("Thank you for trying out version 1 of this game! You played " + timesplayed
                + " times! Enjoy your day, player!");
}

}

4 个答案:

答案 0 :(得分:0)

如果包含main方法的类名为version,则必须调用

java version

答案 1 :(得分:0)

您已经说过,您已将version.javaopen.java 汇编为 version.classopen.class

您还说version.class包含main() - 方法。因此,您必须运行:

java version

希望这有帮助,玩得开心!

答案 2 :(得分:0)

根据你的问题,我可以看到你创建了两个类,它们是

  1. version.class
  2. open.class
  3. 你从哪里获得“titleScreen”课程?

    只有version.class中有main方法。 因此,version.class的main()方法只能是入口点。

    您可以按如下方式运行程序:

    java version titleScreen

    titleScreen可以是此程序的输入。

答案 3 :(得分:0)

由于您已声明main方法位于“version”类中,如果“titleScreen”是您希望为程序提供的输入,则应输入的命令为

java version titleScreen