Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to run a Java jar file without a MS DOS batch file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 12 February 2013 - 10:32 PM

So I have been working on my game for a little over a month. Even with college, I spend as much time as I can on the game to be bug-free as possible and make sure it is important it has the right mechanics of that particular genre.

 

Now I want to share it with people and make sure they can play it by clicking the jar file instead of the MS DOS batch file I created. This would mean the src folder will not be given along side with the Java jar file. 

 

directory_zps0692df20.png

 

I know for certain it is possible seeing as I have downloaded and use other people jar files and their jar files do not come out a src folder or a batch file. How did they do it?


Edited by warnexus, 12 February 2013 - 10:40 PM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8880

Like
0Likes
Like

Posted 13 February 2013 - 12:56 AM

What does your batch file contain? I'd expect this to be out of your control, because for instance, WinRAR typically associates itself with .jar files by default, which means if the user has his system settings set to user WinRAR (or something else) in precedence to Java for .jar files, double-clicking the file will not run the game, and there's not much you can do about that. You could have an installer which reinstates proper file associations, but that is *very* unusual practice for a game.

 

In this case, though, it seems your file associations are correct, I believe you need to write an additional method - probably to set the entry point - to be able to run from the .jar without invoking java directly (which I guess is what your batch file is doing) but I don't know any further off the top of my head. Did you try reading this?


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Khatharr   Crossbones+   -  Reputation: 3002

Like
1Likes
Like

Posted 13 February 2013 - 02:47 AM

You could have an installer which reinstates proper file associations

angry.png


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#4 angelic999   Members   -  Reputation: 144

Like
0Likes
Like

Posted 13 February 2013 - 03:42 AM

You could add/edit the jar manifest to include/specify which class is the startign point.

 

See http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html for more details.



#5 PurpleAmethyst   Members   -  Reputation: 324

Like
0Likes
Like

Posted 13 February 2013 - 06:17 AM

If file associations are correctly set up I would recommend using OneJar (http://one-jar.sourceforge.net/). I've used this for prototypes. It can even load native libraries such as LWJGL.

Other solutions you could use are:



#6 dmatter   Crossbones+   -  Reputation: 3099

Like
3Likes
Like

Posted 13 February 2013 - 07:58 AM

If you're using Eclipse then you can go to "File->export->Java->Runnable Jar file" to create an executable Jar. If you're not using Eclipse then look for something similar in your IDE. If you're not using an IDE then I highly recommend getting one tongue.png

 

As for shipping the source code - whether you make a runnable JAR or use a batch file you shouldn't need to include a copy the source code. When delivering a batch file all you need to ship with it are the compiled .class files and your batch script can invoke Java on those. My guess is that your current batch file is invoking javac (the java compiler) and compiling the source on the fly? Or perhaps you don't realise that once the .class files are generated Java doesn't require the .java source files to actually run the program.



#7 shacktar   Members   -  Reputation: 750

Like
1Likes
Like

Posted 13 February 2013 - 09:23 AM

 

One tool I would recommend for this purpose is Launch4j.

@warnexus It looks like you're targeting Windows, but if you target other platforms you could build one executabe wrapper for each platform.
 
I once had a problem where I created an executable JAR file that worked by double-clicking on my computer but it didn't work on another computer. It could have been an issue involving a JRE difference. The other advantages of using a tool such as Launch4j is that JRE differences are handled automatically (you either bundle your JRE version or you set a min and max version) and you don't have to worry about the user having to set the Java path.


Edited by shacktar, 13 February 2013 - 09:40 AM.


#8 Glass_Knife   Moderators   -  Reputation: 4443

Like
0Likes
Like

Posted 13 February 2013 - 10:09 AM

Now I want to share it with people and make sure they can play it by clicking the jar file instead of the MS DOS batch file I created. This would mean the src folder will not be given along side with the Java jar file.

I'm just wondering if you are having a problem with the OS not executing the jar file when clicked, or are you not setting the Main-Class: in the manifest file of the class path. As pointed out above, if the user doesn't have the file associations configured, there is not much you can do. However, there is nothing wrong with having a batch file or shell script to launch your program. ANT does this, for example.

Using launch software to generate an executable for the target OS to launch the Java code behind the scenes seems like overkill for a small game.

Edited by Glass_Knife, 13 February 2013 - 10:09 AM.

I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#9 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 13 February 2013 - 10:38 AM

What does your batch file contain? Did you try reading this?

I actually did come across the same link but my jar cannot execute unless it has the src folder that has the .class files. when I use other people's jar file. I do not even see the .class files only just one .jar file to execute the program.

 

My batch file is basically a .txt file with these commands: Here's a picture of what is inside the .txt

 

On a side note I do not understand what "-cp" means.

 

 

 

batch_zpsec4cb42f.png

 

 

I already written the manifest. then I tried this command java -jar NicholasGame.jar in the command prompt and it worked but for the average user this would be weird to do. If I execute my jar file without the src folder my application becomes like this. 

 

blank_zps0222e1df.png


Edited by warnexus, 13 February 2013 - 11:24 AM.


#10 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 13 February 2013 - 10:44 AM

You could add/edit the jar manifest to include/specify which class is the startign point.

 

See http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html for more details.

I did add the Manifest. Here is what the Manifest looked like. Should it be in the src folder or outside of the src folder? But the manifest did not help me access the jar file. I even tried to java -jar JAR-name (ie java -jar NicholasGame.jar )on the command prompt and it worked but for the average user this is weird to perform just to open a game.

 

manifest_zps0c7b2f74.png


Edited by warnexus, 13 February 2013 - 10:55 AM.


#11 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 13 February 2013 - 11:07 AM

If you're using Eclipse then you can go to "File->export->Java->Runnable Jar file" to create an executable Jar. 

 

As for shipping the source code - whether you make a runnable JAR or use a batch file you shouldn't need to include a copy the source code. When delivering a batch file all you need to ship with it are the compiled .class files and your batch script can invoke Java on those. My guess is that your current batch file is invoking javac (the java compiler) and compiling the source on the fly? Or perhaps you don't realise that once the .class files are generated Java doesn't require the .java source files to actually run the program.

I followed your instructions but I got export errors which is odd because my game runs fine without errors on Eclipse.

 

Here's what export errors showed.

 

exporterrors_zpsf8567826.png

 

I'm not providing the source code to the user. All source code been compiled into .class files. I actually provided what my batch file is doing above. The batch file is basically a .txt that says "java -cp  NicholasGame.jar Game". what exactly does "-cp" mean? I know the batch file is one way which I already performed before making this thread but I have used other people jar files and all I needed to do was click the jar file and run the application and they did not have any .class files with the jar file


Edited by warnexus, 13 February 2013 - 11:07 AM.


#12 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 13 February 2013 - 11:23 AM

I'm just wondering if you are having a problem with the OS not executing the jar file when clicked, or are you not setting the Main-Class: in the manifest file of the class path. As pointed out above, if the user doesn't have the file associations configured, there is not much you can do. However, there is nothing wrong with having a batch file or shell script to launch your program. ANT does this, for example.

Using launch software to generate an executable for the target OS to launch the Java code behind the scenes seems like overkill for a small game.

Now it seems I cannot execute the jar unless it has the src file(which has the .class files) along side with it. I think when I use other people jar files, the programmers already put the src file inside the jar file. I want to know how I can do that.

 

If there is nothing wrong for the user to click the batch file then I will just let them do that. The reason why I did not want to do it that way was because I did not need to run a batch file when I used other people's jar files.



#13 Glass_Knife   Moderators   -  Reputation: 4443

Like
1Likes
Like

Posted 13 February 2013 - 11:48 AM

what exactly does "-cp" mean?

The -cp flag is short for -classpath, which tells the java command where to look for the class files. If you didn't put *.jar file in the classpath, then Java would not be able to find the code.

The source files do not have anything to do with the jar file working. A jar file is just a *.zip file renamed to *.jar. You could create the jar file as a zip file by hand, including the source code, and then change the extension to *.jar.

However, most people, for creating jar files like this, either do it from the command line or use http://ant.apache.org/

Something looks weird with your class files. They need to be in the package folders in the jar, and you shouldn't be using classes without at least one package. In the past, I have had trouble getting code without any packages to work like this. It seems like something is wrong, because I see your game is called Game, but I also see a Game folder in the jar screenshot.

One thing, is that the main class needs the fully qualified java name. For example, a typical manifest for my games looks like this:

Main-Class: tim.game.app.ReallyAwesomeGame

// ReallyAwesomeGame.java
package tim.game.app; // VERY IMPORTANT!!!

When compiled there with be a folder for each package level, and the Really AwesomeGame file must be in the package structure inside the jar file.

tim
- game
- app
ReallyAwesomeGame

If this stuff isn't exactly right, it won't work.
I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#14 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 13 February 2013 - 12:54 PM

Something looks weird with your class files. They need to be in the package folders in the jar, and you shouldn't be using classes without at least one package. In the past, I have had trouble getting code without any packages to work like this. It seems like something is wrong, because I see your game is called Game, but I also see a Game folder in the jar screenshot.

 

Game.class is the entry point. I have another folder called "Game" and that contains an image for the gameover. I guess I should change the name of the folder to avoid confusion


Edited by warnexus, 13 February 2013 - 01:20 PM.


#15 Glass_Knife   Moderators   -  Reputation: 4443

Like
0Likes
Like

Posted 13 February 2013 - 01:40 PM

The last time I tried to create an executable jar with classes that we not in any packages, I couldn't get it to work. It seemed like a bug, but putting them in at least one package can't hurt.

Try it and see.
I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#16 warnexus   Prime Members   -  Reputation: 1437

Like
0Likes
Like

Posted 14 February 2013 - 10:10 AM

The last time I tried to create an executable jar with classes that we not in any packages, I couldn't get it to work. It seemed like a bug, but putting them in at least one package can't hurt.

Try it and see.

when you're talking about one package, can I use the default package or should I create my own package? I will try this.



#17 Glass_Knife   Moderators   -  Reputation: 4443

Like
0Likes
Like

Posted 14 February 2013 - 10:33 AM

when you're talking about one package, can I use the default package or should I create my own package? I will try this.

create at least one package. The name doesn't really matter. Long ago, in the Java 4.1 days, I tried created a Jar with files that had no package (because I was being lazy and didn't want to create a folder) but it didn't work. That may have been fixed. I've always used at least one package since, and have never tried it without one.
I think, therefore I am. I think? - "George Carlin"
Indie Game Programming




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS