r/javahelp • Nooblet Brewer • May 08 '19

How to make program usable across multiple computers?

I want to eventually compile my program into a .jar file. And ideally, I would also make .exe/.app versions so that people (specifically my grandparents and less tech oriented friends lol) can try it without having java.

I'm working on both Ubuntu and Windows, and when I switch back and forth I need to repath my images. My program ultimately needs to work on a windows computer for my final project. But I am worried because my path applies only to my computers. Our teacher recommended that we compile our program into a .jar file so we dont have to worry about little complications beforehand.

Once my program is saved into a .jar file, will I need to worry about the paths still? Sorry, I'm super naive and have not done this before.

17 Upvotes

18 comments sorted by

7

u/butcanyoufuckit May 08 '19 edited May 08 '19

One way to do it is to check system.env for which one you're on. Then deal with the path and delimiters. Another is to pack it in the jar so you can reference the resource relative path

The differnece is in the details of what you are trying to do. It sounds like the latter. So you want to put the image in your resources folder

Then maybe try something like launch4j.

2

u/yellowpufferfish Nooblet Brewer May 08 '19

Oh okay, so I can "pack" my images in the jar so it will have the files with it? I guess I need to learn how to build a .jar file period

3

u/deathmetal27 Extreme Brewer May 08 '19

Maven has plugins that will allow you to build jars, I personally prefer to use the Maven Shade plugin.

However, you can also build a jar using eclipse by going to File > Export > Jar file.

2

u/butcanyoufuckit May 08 '19 edited May 08 '19

Yep it's relatively easy. At least its gotten easier. You can google how. Amd launch4j should make it more like an exe

https://introcs.cs.princeton.edu/java/85application/jar/jar.html https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file http://launch4j.sourceforge.net/

Now personally I'd use maven (and shade plugin and launch4j) if your jar has dependencies you want to pack wirh it. But that's a whole 'nother rabbit hole for another time that you might wanna look into

1

u/deathmetal27 Extreme Brewer May 08 '19

I think a simpler solution than using launch4j is to create a shell script that will run the jar. Note that using launch4j does not remove the need for a JVM to run the java application.

1

u/Tha_Gazer May 08 '19

upvote. Nice thing about java is it can run on any machine regardless of OS, so long they have java installed. Package the image in with the jar and all is good. Only need to reference the resource relative path

4

u/deathmetal27 Extreme Brewer May 08 '19 edited May 08 '19

File paths aren't really a problem in java across platforms because java will accept forward slashes as a valid separator in Windows. If for some reason you still need to maintain OS specific file paths, you can use:

File.separator

to get the separator character (forward or backward slash).

However, unless you want to do something really OS specific, such as running commands on the underlying OS shell, your code will run the same on all platforms.

Edit: Another thing to note is that since your images will be in different paths on windows and linux, you should use a properties file to configure your application at runtime, this way you don't have to hardcode your path in the code and configure it in the properties file before running your application. This way, you can give any OS specific path format and it will be accepted.

Edit2:

Once my program is saved into a .jar file, will I need to worry about the paths still

Yes, you have to. Though, if your images are fixed, that is you are not going to be adding or removing them dynamically, you can put them into a resource directory in your project itself and package it into the jar. This way you don't have to worry about the paths, but you cannot add/remove images easily since it is all packaged.

Another way is to use the properties file as I have mentioned above.

1

u/yellowpufferfish Nooblet Brewer May 08 '19

Oh neat, I didnt know that. I guess now what I'm wondering is how specific do my paths need to be? Is the folder and image enough? Thanks for your help.

1

u/deathmetal27 Extreme Brewer May 08 '19

Your path can be absolute path or relative to the classpath of your application. If you put the file in the root of your application, you can read it using

File f = new File("filename.jpg")

or

File f = new File("images/filename.jpg");

if your files are stored in a subdirectory.

If you want to read files from some path other than your application's path, you can give an absolute path like:

File f = new File("/home/username/images/filename.jpg");

or in windows like:

File f = new Fle("D:/images/filename.jpg");

However, using this approach means that your paths will be hardcoded and if you want to change the path in the future, you will have to rewrite your code. That is why I recommended using a Properties file in this case. You can read a properties file like any file and then load it into a Properties object.

3

u/_cjj May 08 '19

This is a problem that shouldn't exist, and it's the reason why you use Resources.

The only excuse for using resources outside of your jar is if you are interacting with the file system for a specific reason (e.g a program that reads the user's documents folder).

1

u/yellowpufferfish Nooblet Brewer May 08 '19

I'm a little confused what you mean by "this is a problem that shouldn't exist" I havent actually turned my program into a jar file yet. I'm premeditating the problem. Or do you mean I should always have the files pathed inside the folder with my .java file?

1

u/_cjj May 08 '19

If the resources belong with the application, they should be compiled with it (exist in the jar).

This is the reason we have a Resources directory (or META-INF), so that the images/CSS/properties that your application needs are bundled with it.

This means that the location of these are consistent, regardless of the environment. One of the strengths (and, arguably, purposes) of Java is that it's cross platform compatible, so you should avoid affecting that unless the environment is a factor/variable.

1

u/yellowpufferfish Nooblet Brewer May 08 '19

Is the Resources directory inherent or only to certain IDEs? I use eclipse sometimes but for the most part use vim. Also thanks for your help

1

u/_cjj May 08 '19

It's not an IDE thing. Google will help you here. It's of the fundamental concepts imo - without it, you'll spend forever trying to read files and debug filenotfound exceptions

1

u/yellowpufferfish Nooblet Brewer May 08 '19

Thanks a lot! I'm glad I know this now so I save myself a lot of trouble going forward.

4

u/rayv12 May 08 '19

The easy way to do this is to have the folder your image is in parallel to the jar file. Example:

Say in the code you call the image like this:

pic = new ImageIcon("Pics/buttonpic.jpg"); This means your picture would be called buttonpic and is located in a folder called Pics in your project directory. Now after you make your jar file, copy that Pics folder into the same folder your jar file is located.

1

u/rayv12 May 08 '19

Yes you need to specify paths of the external files to read or write etc. on your new system.

1

u/yellowpufferfish Nooblet Brewer May 08 '19

How do I go about doing that? I'm using images I drew. When I already have them on my computer it's no problem, but how do I make it so that my program can use my images on different computers ? So I could send the .exe/.jar file to people so they can use the program.