Java新手求助:Jar文件双击无响应、Cmd提示无法访问的问题
Hey there! Let's break down your problem step by step—first fixing the code logic, then sorting out the JAR file access error you're seeing.
Looking at your code, there's a small but critical mistake that's making your program behave unexpectedly, even before we get to the JAR file part:
class Vedant { public static void main(String[] args) { int a; a = 1; if (20>a); // ❌ This semicolon ends the if statement immediately! a = 5; { System.out.println("Number ="+a); } } }
That semicolon after the if (20>a) means the if condition doesn't actually do anything—your code will always set a = 5 no matter what, then run the print statement. Here's the corrected version with proper if-block structure:
class Vedant { public static void main(String[] args) { int a = 1; // Initialize the variable in one line for clarity if (20 > a) { // Remove the semicolon and wrap your code in braces a = 5; System.out.println("Number = " + a); // Added a space for readability } // Optional: Add an else case to handle when the condition isn't true else { System.out.println("Number = " + a); } } }
Test this corrected code in BlueJ first to make sure it runs as expected before moving on to packaging.
Now let's tackle the JAR file issue—this usually comes down to one of three common problems:
Incorrect file path in Command Prompt
If your JAR file is in a folder with spaces (likeC:\My Java Projects\Vedant.jar), you need to wrap the path in quotes when running it. Or, navigate to the folder first:# Option 1: Navigate to the folder cd C:\My Java Projects java -jar Vedant.jar # Option 2: Use full path with quotes java -jar "C:\My Java Projects\Vedant.jar"Double-check that you're typing the filename exactly (capitalization matters on Windows for some setups!).
Missing main class in the JAR file
BlueJ won't automatically set the main class when creating a JAR—you have to specify it manually:- In BlueJ, right-click your
Vedantclass (the one with themainmethod) - Select Create Jar File... from the menu
- In the popup window, check the box that says Use Main Class and make sure
Vedantis selected - Choose where to save the JAR and click OK
Without a main class specified, Java doesn't know which code to run when you double-click or usejava -jar.
- In BlueJ, right-click your
Corrupted JAR or Java version mismatch
Even if you installed the latest Java, confirm it's properly set up:- Open Command Prompt and run
java -versionto verify the installed version (should show something likeopenjdk 21.0.1or similar) - Delete your existing JAR file and re-create it using the steps above—sometimes the packaging process can fail silently if there's an error in the code.
- Open Command Prompt and run
Once you've corrected the code and re-packaged the JAR with the main class specified:
- Run it in Command Prompt using the correct path
- If double-clicking still doesn't work, check that Windows is associating
.jarfiles with Java:- Right-click the JAR file -> Open with -> Choose another app -> Select Java(TM) Platform SE binary and check "Always use this app to open .jar files"
内容的提问来源于stack exchange,提问作者Vedant

