I need some help with using a JAR file - java

Okay I understand this might be a simple question, but I was trying to figure out how to make a JAR file so that other people can run my program without having a software like jGrasp, eclipse, or netbeans. The problem I'm having is I save it as a JAR file and can execute it on my computer but when I email it to my friend, it says invalid or corrupt JAR file. Can anyone explain? Thanks

Are you creating the file with the jar command? Does "jar tvf fn.jar" show you the contents? If so, the problem is probably with the email transfer. Keep in mind that a jar file is actually just a zip file with some special data in it, and some email servers will refuse to pass along zip files because of all the trojans that use emailed zip files.
One common problem would be if your email client isn't mime encoding it, so something along the way changes 0x0D (carriage returns) to 0x0D 0xOA (carriage return + line feed) or vice versa, which corrupts the hell out of binary files.

Related

extract contents of .pkg file in java

I have been trying to unpack a .pkg file, which contains two folders and a .json file, into a directory using Java. I have used the apache.commons.compress library in the past, but unable to use it for the .pkg file. Please let me know how i can do this.
Many thanks
Managed to unpack. The .pkg file was actually a .cpio payload file with some header and tail bytes.

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

Reading resources of another jar file

I have three java jar files which can be run independently. Each file contains images resource. I want each of the programs to be able to read resource from each other program. Let's assume they are in the same directory, when the program "A" is run I want to be able to read resources file inside file "B" or "C".
There may be a more elegant way of doing this with classloaders/etc.
However, JAR files are simply ZIP files with a different name. Assuming you know in advance the location of the folder in the JAR (probably src/main/resources), you can simply unzip and read from there.
The class ZipEntry will allow you to do this, without needing any extra libraries: https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html
Like I say, however, there is likely a better way of doing this.

How do you download a a jar file with java?

I'm trying to implement an updates feature in my program. If an update is available, there will have to be one if not several different .jar files that must be downloaded. I'm having problems developing a way to download these files. I've looked at the URLConnection API as well as other API's related to this. I can download regular text files with no problem, but when I need to download a jar file it just doesn't download. No error is thrown, the data just isn't transferred.
If anyone can point me in the right direction to successfully download jar files I would greatly appreciate it.
There is nothing special about JAR files themselves. Transfer it just like any other file (FileInputStream -> SocketOutputStream: SocketInputStream->FileOutputStream). The trick is to use the ClassLoader to link the classes into your running program after you have saved the JAR to disk.

Transfering files from a zip to a jar

I've been troubled for the past 3 days now by an annoying problem, I do not have the faintest clue on how i could possibly transfer files from a zip to a jar. I'm not sure whether or not i need to supply a buffer to said process, and whether i use the JarInput/OutputStream or i instead use Enumerations, I've done some research on how to do it and i cannot find anything apart from a big paste of code, as you can tell i would much prefer an explanation of what i need to do as a pose to how to do it.
Basically i would just like a nice simple, yes informative tutorial on how i could transfer the contents of lets so "foo.zip" into "bar.jar". ("bar.jar" already contains files which i do not want to delete)
To transfer all files from zip to jar you just have to rename zip to jar because jar is a zip format.
EDIT:
If you want to copy some entries from zip/jar to other zip/jar or to copy entries to existing zip/jar you have to iterate over entries in input file using ZipInputStream, read the entries and write them to output ZipOutputStream. Take a look on this tutorial: http://java.sun.com/developer/technicalArticles/Programming/compression/. It explains everything in details and contains code samples.
You can actually just rename the file from foo.zip to foo.jar. I'm not kidding!
You can read and write zip and jar files with java.util.zip, jar files are really just zip files.

Categories

Resources