3

I had written a program:

public class SystemShutdown {
    public static void main(String[] args) {
        try{
            for(int i=0;i<10;i++){
                Thread.sleep(1000);
            }
            Process p=Runtime.getRuntime().exec("shutdown -s -t 2700");
        }catch(Exception e){}
    }
}

I'd compiled and kept the .class file separate.

Now, I'd write a manifest file as:

Manifest-Version: 1.0
Main-Class: SystemShutdown

And saved with the name MANIFEST.MF

I'd put both (the .class file and the MANIFEST.MF file) in same directory.

Now I want to create an Executable Jar file. For that I'd done:

jar cvfm MyJar.jar *.*

A jar file is created after that. But when I tries to execute it displays a message Java Exception occured.

Can anybody help me out? I want to execute this program on the users double click.

Beside of the above scratch can anybody tell me the exact steps to be followed to create an executable jar?

I'm using Windows7 32bit and jdk7

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117

1 Answers1

9

The m option of the command line for jar says you'll provide the manifest file as the following parameter (in this case, after the jar file itself). So I suspect you want:

jar cvfm MyJar.jar MANIFEST.MF SystemShutdown.class

See the jar tool documentation for more details.

EDIT: I've just tried this and it works fine. Code:

// In Test.java
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

// Manifest in MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: Test

Command line and output:

javac Test.java
jar cvfm test.jar MANIFEST.MF Test.class
java -jar test.jar
Hello

Note that if you don't have a line terminator at the end of the Main-Class line in the manifest, that will cause an error, but it's somewhat better specified:

Failed to load Main-Class manifest attribute from test.jar
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • still the problem is as it is – Mohammad Faisal Sep 13 '11 at 16:22
  • @Mohammad: Well I just tried exactly the same thing, and it worked fine. If it's failing for you, perhaps it's a JRE problem? What platform are you on? Have you tried running a similar test app which *doesn't* try to shut the machine down? – Jon Skeet Sep 13 '11 at 16:23
  • did you get your `jar` executed? – Mohammad Faisal Sep 13 '11 at 16:27
  • Yes That is Jon's `java -jar test.jar` command on the next to last line in the penultimate code segment. – Captain Giraffe Sep 13 '11 at 16:30
  • @Mohammad: How are you trying to execute this? As I asked before, what platform are you on What version of Java are you using? – Jon Skeet Sep 13 '11 at 16:36
  • @Mohammad No I guess we would expect a permissions problem rather. – Captain Giraffe Sep 13 '11 at 16:36
  • @Jon: i'm using `jdk7` on `Windows7` OS. When the `jar` file is created then I simply double-clicks on that, but it says `A Java Exception has Occured.` with entitled `Java Virtual Machine Launcher` But past few days I'd downloaded an executable `jar` file which executes well. – Mohammad Faisal Sep 13 '11 at 16:42
  • @Mohammad: Right, so try running it from the command line like I have. You may get more information that way. – Jon Skeet Sep 13 '11 at 16:44
  • @Jon I'm not that familiar with Windows (console), but wouldn't `start TheJar.jar` be more like a double click? – Captain Giraffe Sep 13 '11 at 16:47
  • @Captain Giraffe: Quite possibly - I suspect it won't attach to the console as `java -jar jarfile.jar` will, which is why I suggested using `java` directly... we *want* more information. – Jon Skeet Sep 13 '11 at 16:49
  • @jon: when i'm trying `java -jar MyJar.jar` there is nullpointerException – Mohammad Faisal Sep 13 '11 at 16:50
  • 1
    @Mohammad: Well there we go. More information. Please show the full stack trace of the NullPointerException. – Jon Skeet Sep 13 '11 at 16:51
  • @jon: the stack trace is: `sun.launcher.LauncherHelper.getMainClassFromJUar(Unknown Source)` and `sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)` – Mohammad Faisal Sep 13 '11 at 17:14
  • @Mohammad how did you start that jar? And please edit your question to add this info. – Captain Giraffe Sep 13 '11 at 17:30
  • @Captain: please tell me how to create an executable jar. Tell me the steps what you follows!!! I also wanted to know about `META-INF` folder – Mohammad Faisal Sep 13 '11 at 17:35
  • @Mohammad: That sounds very odd indeed... I've just tried it again at home and it's worked fine here too... – Jon Skeet Sep 13 '11 at 17:40
  • @Mohammad: My answer shows the exact steps necessary to create and execute a jar file. If it's not working for you, that suggests there's something wrong with your environment. – Jon Skeet Sep 13 '11 at 17:40
  • @Mohammad You wont get better answers from me than from Jon. Jon provided his answer. It would be like asking Newton about General relativity when Albert is sitting right next to you. Manual tip of the hat to Jon. – Captain Giraffe Sep 13 '11 at 18:18
  • @Mohammad: So what did you change? – Jon Skeet Sep 14 '11 at 05:18
  • @Jon: previously I'm using jdk7 but don't know what went wrong. now I'd created jar using jdk6 – Mohammad Faisal Sep 14 '11 at 05:28
  • @Jon: can you please tell me how can i change the icon of an executable jar? – Mohammad Faisal Sep 15 '11 at 13:41
  • @Mohammad: I'm afraid I don't know. I suggest you ask a new question - it's likely to be OS-specific. – Jon Skeet Sep 15 '11 at 13:47
  • @Jon: I want to minimize my `JFrame` to the `system tray icon` in `windows`. Do you have any answer post it at http://stackoverflow.com/questions/7461477/move-a-jframe-to-tray-on-minimize – Mohammad Faisal Sep 18 '11 at 12:38
  • 1
    @Mohammad: Please don't just keep adding comments with extra questions which are irrelevant to this one. If I see a question I can answer, I'll do so - if *everyone* kept drawing my attention to their question, I'd have far too many comments to read. – Jon Skeet Sep 18 '11 at 12:48