0
votes

im trying to get my calculator to work on CMD but how do i Get it to work on CMD but i already have put imports and the use within the code. so how do i get it to work on cmd.

heres the code

import java.util.Scanner;
import java.lang.Thread;

public class Calculator {

    int B1;
int C1;
int D1;

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    System.out.println("How May i asist you with your calculation.");
    Thread.sleep(1000);
    System.out.println("So what opperation do you want to use. ");
    Scanner A1 = new Scanner(System.in);
    String in = A1.nextLine();
        if (in.equals("+")){
            System.out.println("Enter the first Number. ");
            Scanner Z = new Scanner(System.in);
            int B1 = Z.nextInt();
            System.out.println("Enter The last Number. ");
            Scanner Y = new Scanner(System.in);
            int C1 = Y.nextInt();
            int D1 = B1 + C1;
            System.out.println(" you answer is " + D1 + ".");}
        else if (in.equals("-")){
            System.out.println("Enter the first Number. ");
            Scanner Z = new Scanner(System.in);
            int B1 = Z.nextInt();
            System.out.println("Enter The last Number. ");
            Scanner Y = new Scanner(System.in);
            int C1 = Y.nextInt();
            int D1 = B1 - C1;
            System.out.println(" you answer is " + D1 + ".");}
        else if (in.equals("*")){
            System.out.println("Enter the first Number. ");
            Scanner Z = new Scanner(System.in);
            int B1 = Z.nextInt();
            System.out.println("Enter The last Number. ");
            Scanner Y = new Scanner(System.in);
            int C1 = Y.nextInt();
            int D1 = B1 * C1;
            System.out.println(" you answer is " + D1 + ".");}
        else if (in.equals("/")){
            System.out.println("Enter the first Number. ");
            Scanner Z = new Scanner(System.in);
            int B1 = Z.nextInt();
            System.out.println("Enter The last Number. ");
            Scanner Y = new Scanner(System.in);
            int C1 = Y.nextInt();
            int D1 = B1 / C1;
            System.out.println("You answer is " + D1 + ".");}
        else
            System.out.print("That's Not a valid operation.");

        }
    {
}

}

heres the error on cmd

Exception in thread "main" java.lang.NoClassDefFoundError: workspace\Calculator\ bin\Calculator (wrong name: Calculator) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

2
You are calling the class with a wrong name: "workspace\Calculator\ bin\Calculator ". By what command you are calling it? Plain java? - robermann

2 Answers

0
votes

You should use Eclipse to compile and export the jar file. Then open it in cmd with: java -jar "YourJarFile.jar" To open cmd in the current directory where your jar file is in, use Shift + Right Click Then select Open Command Window Here

And you don't need to make a new Scanner object each time, you can use it again.

0
votes

Let say Calculator class is located at path c:\\workspace

Open the CMD

c:\workspace> javac Calculator.java // Check .class file created at this location

c:\workspace> java Calculator