user3528349
user3528349

Reputation: 21

I am facing a error when I compile the source code, It is throwing an error that is given below

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

Upvotes: 2

Views: 61

Answers (1)

Snicolas
Snicolas

Reputation: 38168

Put the file Factorial.java inside a folder called myPack.

Then launch javac using : javac HelloWorld.java

In java, the file structure has to follow packages. See this tutorial for more information.

Upvotes: 1

Related Questions