Java没有编译我的源代码

时间:2014-10-01 13:47:56

标签: java eclipse compilation

我无法在命令行中编译我的类。我必须遗漏一些非常明显的东西,但我无法弄明白。

这是我的设置......

环境设置(Win 8.1):

JAVA_HOME   C:\Program Files\Java\jdk1.8.0_11

Path    %JAVA_HOME%\bin;

我有“javaTest”文件夹,其中包含两个java文件Run.java和Animal.java。 我可以编译Animal.java但编译“Run.java”文件时出错。

命令行:

C:\>cd C:\javaTest

C:\javaTest>dir
 Volume in drive C is Local Disk

Directory of C:\javaTest

01/10/2014  10:37 AM    <DIR>          .
01/10/2014  10:37 AM    <DIR>          ..
01/10/2014  10:37 AM               337 Animal.java
01/10/2014  10:36 AM               230 Run.java
               2 File(s)            567 bytes
               2 Dir(s)  42,796,736,512 bytes free

C:\javaTest>javac Animal.java (**note: this gets executed without problems**)

C:\javaTest>javac Run.java
Run.java:5: error: cannot find symbol
                Animal a1 = new Animal();
                ^
  symbol:   class Animal
  location: class Run
Run.java:5: error: cannot find symbol
                Animal a1 = new Animal();
                                ^
  symbol:   class Animal
  location: class Run
Run.java:9: error: cannot find symbol
                Animal a2 = new Animal("Animal2");
                ^
  symbol:   class Animal
  location: class Run
Run.java:9: error: cannot find symbol
                Animal a2 = new Animal("Animal2");
                                ^
  symbol:   class Animal
  location: class Run
4 errors

以下是实际代码:

Run.java

    public class Run{

        public static void main(String[] args){

            Animal a1 = new Animal();
            a1.setName("Animal1");
            a1.showName();

            Animal a2 = new Animal("Animal2");      
            System.out.println(a2.getName());

        }

    }

Animal.java

    public class Animal{

        private String name;

        public Animal() {

        }
        public Animal(String name) {
            this.name = name;

        }

        public void setName(String name){
            this.name = name;
        }

        public String getName(){
            return this.name;       
        }


        public void showName(){
            System.out.println(this.name);      
        }

    }

我可以从Eclipse运行完全相同的代码而没有任何问题。所以我不确定我错过了什么......

1 个答案:

答案 0 :(得分:4)

您必须将实际文件夹添加到类路径中:

javac -cp .\;%CLASSPATH% Animal.java