Reputation: 55
I can't get my .jar files to run. I have read many similar threads but still can't get it working. I can get it to run from the cmd line if I run "java -jar jar_name.jar" from the folder that contains the file. I ran the Jarfix program that is supposed to fix the file association, but it still does not work. When I export from Eclipse it tells me
JAR export finished with warnings. See details for additional information....Exported with compile warnings: Shutdown/src/Shutdown.java
From what I've read, this is not a real issue, it just means there are warnings in your program. I don't think there's any problem with the code, but it's a small program so I've included it anyways. Any help is appreciated.
import java.io.IOException;
import java.util.Scanner;
public class Shutdown {
public static void main(String[] args) throws IOException
{
String os;
String Win = "Windows";
String choice;
os = System.getProperty("os.name");
if(os.contains(Win))
{
System.out.println("Congratulations, you are running "+ os + ".\nYou now have three options.");
do
{
PrintMenu();
Scanner keyboard = new Scanner(System.in);
choice = keyboard.next();
ReturnMenu(choice);//passes user input as argument to method
}
while(choice !="1" || choice != "2" || choice !="3");
}
else
System.out.println("You Are Using A Non-Windows System. Please upgrade.");
}
public static void shutdown() throws IOException
{
Runtime run = Runtime.getRuntime();
Process proc = run.exec("shutdown -s -t 0");
}
public static void restart() throws IOException
{
Runtime run = Runtime.getRuntime();
Process proc = run.exec("shutdown -r -t 0");
}
public static void logoff() throws IOException
{
Runtime run = Runtime.getRuntime();
Process proc = run.exec("shutdown -l");
}
public static void PrintMenu()
{
System.out.println("Please make a selection:"
+ "\n1 - Shut down\n2 - Restart\n3 - Log Off\n");
}
public static void ReturnMenu(String in) throws IOException, NumberFormatException
{
int x;
try
{
x = Integer.parseInt(in);//cast user input to int to be used in switch statement
}
catch (NumberFormatException e)//catches non-number input that can't be case to int
{
x=4;//caught exception sets x to 4 to cause loop to keep running
}
switch (x)
{
case 1:
shutdown();
break;
case 2:
restart();
break;
case 3:
logoff();
break;
default:
System.out.println("Invalid menu selection. Please try again.");
}
}
}
Upvotes: 1
Views: 533
Reputation: 44292
On Windows, a standard Java installation associates double-clicking of .jar files with the javaw.exe
program, not java.exe
. The difference is that javaw.exe has no console (command window).
Since you have no console, you have no System.in
. I haven't had a chance to try it, but my guess is that attempting to read from System.in will result in immediately reaching the end of the stream, which will cause a Scanner's next* methods to throw a NoSuchElementException.
Im summary, you can't use System.in. If you want to prompt the user for input, use Swing or JavaFX.
Additional note: You must not compare Strings using ==
or !=
in Java, as those operators compare object references instead of comparing String values. Use the equals
method instead. For instance:
while (!choice.equals("1") && !choice.equals("2") && !choice.equals("3"))
Upvotes: 0
Reputation: 6608
Select your File/Project -- Export -- Runnable JAR -- Select class with main() in
Launch Configuration -- destination
Hope this helps :)
Upvotes: 1