尝试将项目导出为 JAR 时,文件在启动时立即被视为已损坏
When trying to export a project as a JAR the file is instantly viewed as corrupt upon launch
我在 Java 中遇到导出问题。当我启动我创建的 JAR 文件时(在 Eclipse 中),它立即给我一个错误,阅读:
"Error: Invalid or corrupt jarfile C:\Users\Samuel\Desktop\Deep Dungeons.jar"
我的代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
// System Objects
Scanner in = new Scanner(System.in);
Random rand = new Random();
// Game variables
String[] enemies = {"Skeleton" , "Zombie", "Warrior", "Assassin"};
int maxEnemyHealth = 75;
int enemyAttackDamage = 25;
// Player Variables
int health = 100;
int attackDamage = 50;
int numHealthPotions = 5;
int healthPotionHealAmount = 30;
int healthPotionDropChance = 50; // Percentage
int enemiesKilled = 0;
int maxHealth = 100;
boolean running = true;
System.out.println("Welcome to the Dungeon!");
GAME:
while(running) {
System.out.println("--------------------------------------");
int enemyHealth = rand.nextInt(maxEnemyHealth);
String enemy = enemies[rand.nextInt(enemies.length)];
System.out.println("\t# " + enemy + " has appeared! #\n");
while(enemyHealth > 0) {
System.out.println("\t Your HP: " + health);
System.out.println("\t " + enemy + "'s HP: " + enemyHealth);
System.out.println("\n\tWhat would you like to do?");
System.out.println("\t1. Attack");
System.out.println("\t2. Drink health potion");
System.out.println("\t3. Run!");
String input = in.nextLine();
if(input.equals("1")) {
int damageDealt = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageDealt;
health -= damageTaken;
System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage");
System.out.println("\t> You recieve " + damageTaken + " in retaliation!");
if (health < 1) {
System.out.println("\t You have taken too much damage, you are to weak to go on!");
break;
}
} else if(input.equals("2")) {
if (numHealthPotions > 0) {
health += healthPotionHealAmount;
if (health > maxHealth)
health = 100;
numHealthPotions --;
System.out.println("\t> You drink a health potion, healing yourself for " + healthPotionHealAmount + "."
+ "\n\t> You now have " + health + " HP."
+ "\n\t> You now have " + numHealthPotions + "health potions left.\n");
} else {
System.out.println("\t> You have no health potions left! Defeat enemies for a chance to get one!\n");
}
} else if(input.equals("3")) {
System.out.println("\tYou run away from the " + enemy + "!");
continue GAME;
} else {
System.out.println("\nInvalid command!");
}
}
if (health < 1) {
System.out.println("You limp out of the dungeon, weak from battle.");
System.out.println("You killed " + enemiesKilled + ".");
break;
}
System.out.println("--------------------------------------");
System.out.println(" # " + enemy + " was defeated! # ");
enemiesKilled ++;
System.out.println(" # You have " + health + " HP left #");
if(rand.nextInt(100) > healthPotionDropChance) {
numHealthPotions ++;
System.out.println(" # The enemy dropped a health potion! # ");
System.out.println(" # You have " + numHealthPotions + " health potion(s). # ");
}
System.out.println("--------------------------------------");
System.out.println("What would you like to do now?");
System.out.println("1. Continue fighting");
System.out.println("2. Exit dungeon");
String input = in.nextLine();
while(!input.equals("1") && !input.equals("2")) {
System.out.println("Invalid command!");
input = in.nextLine();
}
if (input.equals("1")) {
System.out.println("You continue on your adventure!");
} else if(input.equals("2")) {
System.out.println("You exit the dungeon, successful from your adventures!");
System.out.println("You killed " + enemiesKilled + "!");
break;
}
}
System.out.println("######################");
System.out.println("# THANKS FOR PLAYING #");
System.out.println("######################");
}
}
我找到问题了。当我导出时,我应该尝试导出为可运行的 jar 文件,而不是常规的 JAR 文件。
我在 Java 中遇到导出问题。当我启动我创建的 JAR 文件时(在 Eclipse 中),它立即给我一个错误,阅读:
"Error: Invalid or corrupt jarfile C:\Users\Samuel\Desktop\Deep Dungeons.jar"
我的代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
// System Objects
Scanner in = new Scanner(System.in);
Random rand = new Random();
// Game variables
String[] enemies = {"Skeleton" , "Zombie", "Warrior", "Assassin"};
int maxEnemyHealth = 75;
int enemyAttackDamage = 25;
// Player Variables
int health = 100;
int attackDamage = 50;
int numHealthPotions = 5;
int healthPotionHealAmount = 30;
int healthPotionDropChance = 50; // Percentage
int enemiesKilled = 0;
int maxHealth = 100;
boolean running = true;
System.out.println("Welcome to the Dungeon!");
GAME:
while(running) {
System.out.println("--------------------------------------");
int enemyHealth = rand.nextInt(maxEnemyHealth);
String enemy = enemies[rand.nextInt(enemies.length)];
System.out.println("\t# " + enemy + " has appeared! #\n");
while(enemyHealth > 0) {
System.out.println("\t Your HP: " + health);
System.out.println("\t " + enemy + "'s HP: " + enemyHealth);
System.out.println("\n\tWhat would you like to do?");
System.out.println("\t1. Attack");
System.out.println("\t2. Drink health potion");
System.out.println("\t3. Run!");
String input = in.nextLine();
if(input.equals("1")) {
int damageDealt = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageDealt;
health -= damageTaken;
System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage");
System.out.println("\t> You recieve " + damageTaken + " in retaliation!");
if (health < 1) {
System.out.println("\t You have taken too much damage, you are to weak to go on!");
break;
}
} else if(input.equals("2")) {
if (numHealthPotions > 0) {
health += healthPotionHealAmount;
if (health > maxHealth)
health = 100;
numHealthPotions --;
System.out.println("\t> You drink a health potion, healing yourself for " + healthPotionHealAmount + "."
+ "\n\t> You now have " + health + " HP."
+ "\n\t> You now have " + numHealthPotions + "health potions left.\n");
} else {
System.out.println("\t> You have no health potions left! Defeat enemies for a chance to get one!\n");
}
} else if(input.equals("3")) {
System.out.println("\tYou run away from the " + enemy + "!");
continue GAME;
} else {
System.out.println("\nInvalid command!");
}
}
if (health < 1) {
System.out.println("You limp out of the dungeon, weak from battle.");
System.out.println("You killed " + enemiesKilled + ".");
break;
}
System.out.println("--------------------------------------");
System.out.println(" # " + enemy + " was defeated! # ");
enemiesKilled ++;
System.out.println(" # You have " + health + " HP left #");
if(rand.nextInt(100) > healthPotionDropChance) {
numHealthPotions ++;
System.out.println(" # The enemy dropped a health potion! # ");
System.out.println(" # You have " + numHealthPotions + " health potion(s). # ");
}
System.out.println("--------------------------------------");
System.out.println("What would you like to do now?");
System.out.println("1. Continue fighting");
System.out.println("2. Exit dungeon");
String input = in.nextLine();
while(!input.equals("1") && !input.equals("2")) {
System.out.println("Invalid command!");
input = in.nextLine();
}
if (input.equals("1")) {
System.out.println("You continue on your adventure!");
} else if(input.equals("2")) {
System.out.println("You exit the dungeon, successful from your adventures!");
System.out.println("You killed " + enemiesKilled + "!");
break;
}
}
System.out.println("######################");
System.out.println("# THANKS FOR PLAYING #");
System.out.println("######################");
}
}
我找到问题了。当我导出时,我应该尝试导出为可运行的 jar 文件,而不是常规的 JAR 文件。