Reputation: 11
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)
Upvotes: 0
Views: 87
Reputation: 4923
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
Upvotes: 0
Reputation: 96
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.
Upvotes: 0