You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导出带用户交互的Java银行模拟程序为可运行Jar文件遇阻

问题描述

我正尝试将Eclipse中的Java银行模拟程序导出为可运行.jar文件到桌面。该程序启动时会显示交互菜单,代码在Eclipse中运行完全正常,但导出为可运行.jar后点击无反应,怀疑是用户交互输入导致的问题。此前我成功导出过无交互的可运行Jar文件,希望点击桌面的Jar文件就能像在Eclipse中一样运行并进行交互。

程序代码

主类 FirstBank

public class FirstBank {

    public static void main(String[] args) {

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

Account类

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("Withdrawn: " + Math.abs(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';
        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("Thank you for banking with us!");              
                System.out.println();
                break;
            default: 
                System.out.println("Invalid input: Please enter A, B, C, D, E, or F.");
                break;
            }
            
        }   while (option != 'F');              
        scanner.close();
        
    }
    
}
解决方案

1. 确认Eclipse导出可运行Jar的步骤正确

  • 右键项目 → 选择Export
  • 在弹出窗口中选择Java → Runnable JAR file,点击Next
  • 确保Launch configuration选择的是主类FirstBank
  • Export destination选择桌面路径
  • Library handling选择Package required libraries into generated JAR,避免依赖缺失
  • 点击Finish完成导出

2. 解决双击Jar无反应的核心问题

你的程序是控制台交互程序,依赖System.in输入和System.out输出。Windows默认双击Jar文件时,会用javaw.exe运行,这个程序不会弹出控制台窗口,导致看不到输出、无法输入,看起来像没反应。

方法一:用命令行运行Jar

打开命令提示符(CMD),切换到桌面目录,执行命令:

java -jar YourJarFileName.jar

即可看到控制台窗口,正常进行交互操作。

方法二:创建批处理文件双击运行

在桌面新建文本文件,输入以下内容:

@echo off
java -jar YourJarFileName.jar
pause

将文件名改为RunBankProgram.bat(后缀改为.bat),双击该批处理文件,会自动打开控制台运行程序,运行结束后窗口会暂停,方便查看结果。

额外优化说明

原代码的getPreviousTransaction方法中,取款操作的提示文本有误,已修正为Withdrawn;同时在showMenu方法末尾添加了scanner.close(),避免资源泄漏。

内容的提问来源于stack exchange,提问作者zach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 06:31:15