简体   繁体   中英

Java - Error trying to access a class from another class

I've been trying to make a programm called QuadraticTester, which calculates quadratic function by using another class called Quadratic. To do that, I ask the user to write three coefficients (A, B and C). Everything works fine when a try to compile them, the problem starts when I want to execute them. This is the error that the CMD shows me when I use the java QuadraticTester command:

C:\Users\Pedro\Desktop>java QuadraticTester.java
Exception in thread "main" java.lang.IllegalAccessError: failed to access class Quadratic from class QuadraticTester (Quadratic is in unnamed module of loader 'app'; QuadraticTester is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @11c20519)
        at QuadraticTester.main(QuadraticTester.java:4) 

This is the Quadratic.java:

class Quadratic{

    double a;
    double b;
    double c;

    String buildEcuation(double a, double b, double c){
        String ecuation =(a + "x2 + " + b +"x  + " + c + " = 0");
        return ecuation;
    }
}

This is the QuadraticTester.java:

class QuadraticTester{
    public static void main(String[] args){

        Quadratic cuad1 = new Quadratic();

        double a = Scanner.getDouble("Ingrese un valor para el coeficiente A: \n");
        double b = Scanner.getDouble("Ingrese un valor para el coeficiente B: \n");
        double c = Scanner.getDouble("Ingrese un valor para el coeficiente C: \n");

        System.out.println(cuad1.buildEcuation(a, b, c));

    }
}

I don't know the meaning of this errors or how to fix them. I hope you can understand my problem and thanks in advance!

PD: I'm not very good at speaking English, so sorry if there is something grammatically wrong in this question;)

In your code are missing some import, the complete Java file could help more.

Assuming that you're using a different Scanner (not the java.util one), the classes seems that are not in the same package, so the QuadraticTester class cannot see the Quadratic one.

Assuming that you want to pass the data from the command line, using the scanner. This is the solution.

public class Test {

    public static void main(String[] args){

        Quadratic cuad1 = new Quadratic();

        Scanner scanner = new Scanner(System.in);
        System.out.println("Ingrese un valor para el coeficiente A: \n");

        double a = scanner.nextDouble();

        System.out.println("Ingrese un valor para el coeficiente B: \n");

        double b = scanner.nextDouble();

        System.out.println("Ingrese un valor para el coeficiente C: \n");

        double c = scanner.nextDouble();

        System.out.println(cuad1.buildEcuation(a, b, c));

    }

}


class Quadratic{

    double a;
    double b;
    double c;

    String buildEcuation(double a, double b, double c){
        String ecuation =(a + "x2 + " + b +"x  + " + c + " = 0");
        return ecuation;
    }
}

A quick tutorial for scanner might be helpful as well. https://www.w3schools.com/java/java_user_input.asp

I had to tweak your code to get to work in my IDE, but mostly what you're doing looks fine. I'd suggest that perhaps your project is setup badly and the class files you're running with are stale.

Reading Oracle's documentation (Java 8) , you can see:

The name must be qualified (§6.5.5.2), or a compile-time error occurs.

What does it mean for something to be qualified? ( see here )

If a type name is of the form Q.Id, then Q must be either a type name or a package name.

If Id names exactly one accessible type (§6.6) that is a member of the type or package denoted by Q, then the qualified type name denotes that type.

If Id does not name a member type within Q (§8.5, §9.5), or the member type named Id within Q is not accessible (§6.6), or Id names more than one member type within Q, then a compile-time error occurs.

In other words, you need to put Quadratic (at the very least) in a package.

Hint : If you don't want to use import statements, put both classes in the same package.

This is kind of embarrasing, but I was writing the execute command in a wrong way:

I was executing like this:

C:\Users\Pedro\Desktop>java QuadraticTester.java

But the correct way is:

C:\Users\Pedro\Desktop>java QuadraticTester

(Without '.java')

Thanks to everybody for your answers and kindness; ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM