0

I am trying to figure out what I am doing wrong?

package com.berrycalc;

import java.text.NumberFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final byte MONTHS_IN_YEAR=12;
        final byte PERCENT=100;

        Scanner scanner=new Scanner(System.in);

        System.out.println("Welcome to Berry Bank! How much money will you need to borrow for your home loan?");
        int principal=scanner.nextInt();

        System.out.println("What is your expected Annual Interest Rate?");
        float annualInterest=scanner.nextFloat();
        float monthlyInterest=annualInterest/PERCENT/MONTHS_IN_YEAR;

        System.out.println("How many years are you expecting to pay on this loan? 30year/15year?");
        byte years= scanner.nextByte();
        int numberOfPayments=years*MONTHS_IN_YEAR;

        double mortgage=principal* (monthlyInterest*Math.pow(1+monthlyInterest,numberOfPayments)
                /(Math.pow(1+monthlyInterest, numberOfPayments)-1));

        String mortgageFormatted=NumberFormat.getCurrencyInstance().format(mortgage);
        System.out.println("Here is your Mortgage Payment ="+mortgageFormatted);
    }
}

Below is the error I am receiving when I try to run the Java program:

 sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
 java -classpath .:target/dependency/* Main
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: com/berrycalc/Main (wrong name: Main)
exit status 1
 ^C
 

Here is my replit link: https://replit.com/join/afsyvxnqas-amandaberry-tec

I copied my code from Visual Studio Code to here but now everything has broken.

  • 1
    `java -classpath .:target/dependency/* com.berrycalc.Main` Your class is in a package, which you need to define to the runtime – g00se Aug 01 '23 at 20:48
  • Also, be careful how that globbed classpath expands. To check it's what you think it is, do `echo ".:target/dependency/*"` – g00se Aug 01 '23 at 20:54
  • Remove "package com.berrycalc;" , it also depends where you start your class from with the OS command interpreters "working directory"(as much in a .bat or .sh). Also, with a jar, the Main-Class: entry in the manifest file must be set. (package declaration on a main class is reasonably irrelevant , if you can't find main you can't find nutt'in!) – Samuel Marchant Aug 02 '23 at 01:15
  • *Remove "package com.berrycalc;"* @SamuelMarchant, you're seriously proposing that as a solution? Also your remark about a jar file are not relevant here. – g00se Aug 02 '23 at 07:22
  • @g00se an interesting feature of his folder structure is it starts with "com" , that is the most common name and recommended name for top folder of a jar library. Of class not found, on a main class it imports it does not get imported unless it's some sort of connector! – Samuel Marchant Aug 02 '23 at 08:03
  • ...anyhow, the direct command should be something like java -classpath "com\berrycalc";. Main (Windows) from the folder above com as the working directory. And similar in Unix java -classpath "com/berrycalc" Main – Samuel Marchant Aug 02 '23 at 08:07
  • *...anyhow, the direct command should be something like...* Absolutely not. Aside from the obvious fact that the OP is not using Windows, the classpath *never* reaches into the package tree – g00se Aug 02 '23 at 08:20
  • The line in his error message with "Caused by" reasonably much says it ! "Main" is not part of the class path folders. – Samuel Marchant Aug 02 '23 at 10:49

0 Answers0