我编译源代码时遇到错误,它抛出了下面给出的错误

时间:2014-04-13 05:41:28

标签: java packages

的java

import myPack.*;
public class HelloWorld{

    public static void main(String args[])
        {
            int a=Integer.parseInt(args[0]);
            Factorial f= new Factorial();
            int d = f.fact(a);
            System.out.println("Factorial of " +a+ " is : " +d);
        }
}

Factorial.java

package myPack;

public class Factorial
{
        public int fact(int b)
    {
        int c=1;
        for(int i=b;i>0;i--)
        {
        c=c*i;
        }
        return c;
    }

}

E:\Packages>javac HelloWorld.java
HelloWorld.java:7: error: cannot access Factorial
                        Factorial f= new Factorial();
                        ^
  bad source file: .\Factorial.java
    file does not contain class Factorial
    Please remove or make sure it appears in the correct subdirectory of the sou
rcepath.
1 error

1 个答案:

答案 0 :(得分:1)

将文件Factorial.java放在名为myPack的文件夹中。

然后使用:javac HelloWorld.java

启动javac

在java中,文件结构必须遵循包。有关详细信息,请参阅此tutorial