Problems creating jar files.

I typically use Eclipse to do all my java programming in and when I use it to test my programs it works great. No problems at all with it so far. My problems are coming in when I try to create an executable jar file. It doesn’t matter if I use Eclipse to make it or do it through command, I always end up with the same error of “Could not find the main class: Main” I have followed written guides on it and even some YouTube videos for a visual example. Why is it that it’s not able to find the main class?

It looks like you’re using the default package. You need to give Eclipse’s jar exporter the full package name of your main class, e.g. com.foo.mygame.Main. If you’re doing it by hand with the jar command, you need to make sure you have a META-INF/MANIFEST.MF file that contains “Main-Class: com.foo.mygame.Main” somewhere in it.

If your Main class, or any other class, is in the default package, then by jove move it the hell out of there. Don’t ever use the default package, it can lead to a lot of subtle weird issues.

I have tried a few variations of that and in every form I’ve tried it always has the same error.

EDIT:

As a small example to try and figure this out I had this code.

package com.game.mygame;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class Main {
 
	public static void main(String[] args) {
 
	    Frame f = new Frame();
	    f.addWindowListener
	          (new WindowAdapter() {
	              public void windowClosing(WindowEvent e) {
	                 System.exit(0);
	                 }
	              }
	    );  
	    f.add(new Label("This JAR file is executable!"));
	    f.setSize(500,500);
	    f.setVisible(true);
	}
}

I was also using this in my manifest.txt file.


Main-Class: com.game.mygame.Main


I have tried making an executable jar file in command, eclipse, and net beans, none of which seem to be able to find the main class.

Here’s what I do when I want to create an executable jar from a new project:

  • In the Package Explorer, right click on the project folder.
  • Run As -> Run Configurations.
  • On the left of the window, right click on Java Application -> New (After this you should have a subset option below Java Application)
  • On the right part of the window you should have a ‘Search’ button next to a ‘Main class’ label. Click on it and start typing the name of your main class. It should appear in the ‘Matching items’ list. Click OK.
  • Click Apply, and Close.
  • Go back to the Package Explorer, right click on the project folder.
  • Export -> Java folder -> Runnable JAR file -> next.
  • In ‘Launch configuration’ select the launch configuration you’ve just created.
  • In ‘Export destination’ choose where you want the JAR file to be saved.
  • If you have referenced libraries in your project, click the option “Package required libraries into generated JAR”
  • Click Finsh.

Hope this helps!

Do you have a copy of the JAR so we can poke through it to see what’s wrong?

It has to be named MANIFEST.MF and be in a directory called META-INF in the root of your jar.

I bet he was talking about the manifest.txt file you give the JAR command line tool :wink:

Right, I always forget about the -m flag. And the error would have been different for a missing main-class entry anyway … ah well, thought I had an easy one for a moment :slight_smile:

I’ve never said that!! ??? ;D

This has it fixed in eclipse at least. now I just have to figure it out for command as I feel it would be needed.

Jar file is simple archive with .jar ending and you can create with any compressor (7zip or other). And to make it executable it should contain META-INF/MANIFEST.MF file that contains “Main-Class:yourClass”

Probably this should´t go here but I didn´t want to create another thread, because I think it fits here. :slight_smile:

I try to create an executable jar using export (in eclipse), but the resulting jar doesn´t run at all. I get no errors. The project runs great inside Eclipse, but I´d like to create the executable jar so I can share it!

I did what H3rnst said but still, it doesn´t run.

The main class:


package AEI.main;

import AEI.engine.GameEngine;


public class Main{
	public static void main(String[] args) {	
		GameEngine game = new GameEngine();
		game.run();
	}	
}

Is it because “run” it´s in the engine? But that should´t be a problem, since it´s called just fine from main :-\

Thanks!

Again, please provide a JAR. Its quite hard to diagnose your problem blindly. :slight_smile:

Try to run it from console with the following command:

java -jar your_game.jar

and it should display some kind of error. Tell us what is it, or do as ra4king says and upload the .jar you’re having problems with.

Thanks mate, somehow the IDE didn´t refresh the sprites folder and was using an “old cached one” (???) so no errors while running from eclipse, but I did what you said and said that it didn´t find the sprites (which I had renamed -_-).

I pressed F5 a lot and finally Eclipse told me the same error, so everything works now, thank you :slight_smile:
(and ra4king, thank you too mate)