Home > Software design >  having trouble exporting java file that prompts user into .jar file onto my desktop
having trouble exporting java file that prompts user into .jar file onto my desktop

Time:10-08

I am trying to export a java file from Eclipse into a runnable .jar file onto my desktop.

The program is a simulation of a bank, and it prompts you with a menu when you start that looks like this:

Welcome John Doe. Your ID is 1.

What would you like to do?

A. Check your balance B. Make a deposit C. Make a withdrawal D. View previous transaction E. Calculate interest F. Exit

Enter an option:

Here is my main class:

public class FirstBank {

    public static void main(String[] args) {

        Account zach = new Account("John Doe", 000001);
        
        zach.showMenu();    
    }
}

and here is a separate class with all of my methods:

import java.util.Scanner;

public class Account {

    int balance;
    int previousTransaction; 
    String customerName;
    int customerID;

    //constructor 
    public Account(String customerName, int customerID) {
        this.customerName = customerName;
        this.customerID = customerID;
    }
    
    
    public void deposit(int amount) {
        if(amount != 0) {
            balance = balance   amount;
            previousTransaction = amount;
        }
    }
        
    public void withdraw(int amount2) {
        if(amount2 <= balance) {
            balance = balance - amount2;
            previousTransaction = -amount2;
        } else {
            System.out.println("Error: insufficient funds.");
        }
    }
    
    
    public void getPreviousTransaction() {
        if (previousTransaction > 0) {
            System.out.println("Deposited: "   previousTransaction);
    }   else if (previousTransaction < 0) {
            System.out.println("Deposited: "   previousTransaction);
    }   else {
            System.out.println("No transaction occurred");
    }
        
    }
    
    public void calculateInterest (int years) {
        double interestRate = .0185; 
        double newBalance = (balance * interestRate * years)   balance;
        System.out.println("The current interest rate is "   (100 * interestRate)   "%.");
        System.out.println("After "   years   " years, your balance will be $"   newBalance  ".");
    }
    
    public void showMenu () {
        char option = '\0';
        // creates a scanner object: 
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Welcome "   customerName   ".");
        System.out.println("Your ID is "   customerID   ".");
        System.out.println();
        System.out.println("What would you like to do?");
        System.out.println();
        System.out.println("A. Check your balance");
        System.out.println("B. Make a deposit");
        System.out.println("C. Make a withdrawal");
        System.out.println("D. View previous transaction");
        System.out.println("E. Calculate interest");
        System.out.println("F. Exit");
        
        do {
            System.out.println();
            System.out.println("Enter an option: ");
            char option1 = scanner.next().charAt(0);
            option = Character.toUpperCase(option1);
            System.out.println();
            
            switch (option) {
            case 'A':
                System.out.println();
                System.out.println("Balance = $"   balance);
                System.out.println();
                break;
            case 'B':
                System.out.println();
                System.out.println("Enter a deposit amount: ");
                int amount = scanner.nextInt();
                deposit(amount);
                System.out.println();
                break;
            case 'C':
                System.out.println();
                System.out.println("Enter an amount to withdraw: ");
                int amount2 = scanner.nextInt();
                withdraw(amount2);
                System.out.println();
                break;
            case 'D':
                System.out.println();
                getPreviousTransaction();
                break;
            case 'E':
                System.out.println();
                System.out.println("Enter the number of years: ");
                int years = scanner.nextInt();
                calculateInterest(years); 
                System.out.println();
                break;
            case 'F':
                System.out.println();
                System.out.println();               
                System.out.println();
                break;
            default: 
                System.out.println("Invalid input: Please enter A, B, C, D, E, or F.");
                break;
            }
            
        }   while (option != 'F');              
            System.out.println("Thank you for banking with us!");   
        
    }
    
}

I tried exporting into a .jar file and a runnable .jar file onto my desktop, but when I click on the jar file it doesn't do anything.

I think the issue may be that the file prompts the user to enter in the information.

Ideally, I would like to just click on the .jar file on my desktop, enter in my information, and have the program run (exactly like it does in eclipse).

The code runs perfectly in eclipse with no issues, and I have exported other code from eclipse before as runnable jars files and they ran perfectly. Except these files did not prompt user input from the user.

Thank you!!!!

CodePudding user response:

To check if Eclipse created the jar file correctly, open a terminal window and run

java -jar <yourjarfile>

If that executes as desired no longer search for the problem on Eclipse. If not, check questions like How can I create an executable JAR with dependencies using Maven?

Now you better check why Windows would execute the jar file. Basically it has to invoke the above command and show the output in a console. But I suspect it would run

javaw -jar <yourjarfile>

which does not open a terminal and thus not show any output. In this case you need to fix the problem in the Windows Explorer, or the extension bindings. Have a look at https://www.winhelponline.com/blog/repair-jar-file-association-windows/

  • Related