Jump to content

Java -- create an executable program

So, I was wondering how I make an executable program from my program I've written in Java so some of my friends can play with it.

This is probably basic knowledge to most of you here, but I have searched for an answer for a few weeks now, until I decided to ask the nice people on this forum.

 

If someone is interested in the source code, it's available on Github : https://github.com/Vogeltak/Hangman

It's just a basic text-based hangman game, for practicing (is it practicing or practising) purposes.

If you have tips on how to improve my code, I would value your feedback! Because I'm sure there are some things wrong with it, or done inefficiently..

 

Thank you  

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

What editor do you use?  

And it's practising.  :P

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

If you use Netbeans, I know it creates a .jar file, which is technically an executable. You just gotta find it in the project folder. 

Link to comment
Share on other sites

Link to post
Share on other sites

What editor do you use?  

And it's practising.  :P

 

I use Eclipse, standard edition.

 

Thank you for correcting me, English is not my native language, so I try to learn as much as possible and hope I'll be somewhat of a native speaker one day :D

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

If you use Netbeans, I know it creates a .jar file, which is technically an executable. You just gotta find it in the project folder. 

 

I use Eclipse though..

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

I use Eclipse, standard edition.

 

Thank you for correcting me, English is not my native language, so I try to learn as much as possible and hope I'll be somewhat of a native speaker one day :D

 

1.  Right-click on your project in Eclipse.

2.  Choose Export.

3.  In the next window, choose the stuff you want.

4.  Click finish.

 

Now a jar file should exist in the destination folder your specified.

Intel i7-870  //  MSI GTX 670 PE       ///       Intel i7-4500U  //  GT720M

Link to comment
Share on other sites

Link to post
Share on other sites

1.  Right-click on your project in Eclipse.

2.  Choose Export.

3.  In the next window, choose the stuff you want.

4.  Click finish.

 

Now a jar file should exist in the destination folder your specified.

 

I created an executable jar file. But it won't execute..

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

What editor do you use?  

And it's practising.  :P

Actually it is dependent on which type of English you are using.  For Canada and parts of the US it is actually practicing, and spelling with an s is actually the out-of-style way of doing it.  Similar to correcting someone who spells meter metre, or center centre...it all depends on where you are from.

 

I created an executable jar file. But it won't execute..

Make sure you do export -> Runnable Jar, and not just jar...If I had to guess this would be the problem.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Actually it is dependent on which type of English you are using. For Canada and parts of the US it is actually practicing, and spelling with an s is actually the out-of-style way of doing it. Similar to correcting someone who spells meter metre, or center centre...it all depends on where you are from.

Make sure you do export -> Runnable Jar, and not just jar...If I had to guess this would be the problem.

I created a runnable jar..

Mh weird..

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

I created a runnable jar..

Mh weird..

I believe you need to need to execute it in the console (cmd, terminal, etc.)

Link to comment
Share on other sites

Link to post
Share on other sites

I believe you need to need to execute it in the console (cmd, terminal, etc.)

Jar files should be able to be executed via double click....I guess if it is a pure console application perhaps it is different, but at least java swings should be able to be executed without going into console

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Jar files should be able to be executed via double click....I guess if it is a pure console application perhaps it is different, but at least java swings should be able to be executed without going into console

 

It is a pure console application, so that is probably the problem. I will try it out tomorrow.

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, well one option would be just create a bat file.  Then in the bat

java -jar [application].jar

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

Ah, well one option would be just create a bat file.  Then in the bat

java -jar [application].jar

 

In the game a list of words gets stored in an array (bufferedReader --> file). How should I refer to the text document and include it in the jar file so it is able to run without accessing files from outside of the jar application?

 

When I create a new FileReader I submit a filepath to it where the text document can be found, what I would like to know is how I can let it refer to the file inside the .jar file.

 

FileReader filePath = new FileReader("D:/Coding/Java/Code_snippets/Hangman_by-vogeltak/src/hangman/Lexicon.txt");BufferedReader br = new BufferedReader(filePath);

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

I am sure there is a way, perhaps someone else who has more java experience can help (I haven't really packed jar files with files before).  Another solution would be to release the files along with the program, and use relative file paths

e.g.

myprogram.jar

assets/Lexicon.txt

assets/anyotherassets.txt

FileReader filePath = new FileReader(".\\assets\\Lexicon.txt");

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Mh, it seems useful. I do not really understand it though. Could you explain what happens when I try this method?

Well, roughly speaking it's a way of getting a resource (e.g. file) based on the classpath instead of on a path (relative or absolute).

 

When you call the JVM with a jar the classpath should be set so that the classes and resources can be loaded by the VM.

Each loaded class has a class loader associated. You get that loader and ask it to load a resource, specifying the relative path to the root of the classpath.

Link to comment
Share on other sites

Link to post
Share on other sites

Ups, double post. Having internet issues.

On that note, excuse my brevity... 

Edited by MikeD
Link to comment
Share on other sites

Link to post
Share on other sites

Well, roughly speaking it's a way of getting a resource (e.g. file) based on the classpath instead of on a path (relative or absolute).

 

When you call the JVM with a jar the classpath should be set so that the classes and resources can be loaded by the VM.

Each loaded class has a class loader associated. You get that loader and ask it to load a resource, specifying the relative path to the root of the classpath.

 

So, because the JVM has the path to the jar file, it can associate the relative filepath you setup with the jar file it should be in? That is how I understood it.

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

So, because the JVM has the path to the jar file, it can associate the relative filepath you setup with the jar file it should be in? That is how I understood it.

I can't research the specifics of how it is done other than what is said here: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

But that should be the case yes, since the folders in the jar are included in the classpath. 

Link to comment
Share on other sites

Link to post
Share on other sites

I can't research the specifics of how it is done other than what is said here: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

But that should be the case yes, since the folders in the jar are included in the classpath. 

 

Thank you very much for helping me.

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×