executable jar from eclipse unable to use images within packages... sometimes - java

I have wrote a swing application and it works fine in eclipse but when I export it as a runnable jar parts of the application fail, when dealing with images, this line for example;
logo = getClass().getResource("/com/cogentautomation/logo.jpg").getPath();
eclipse is packaging the images in the com.cogentautomation package and I can see it in the .jar itself, I have tried both export methods, extract required libraries and package required libraries, one says;
FileNotFoundException com\cogentautomation\logo.jpg
the other says;
FileNotFoundException file:\c:\documents\hs.jar!\com\cogentautomation\logo.jpg
I am using a library to parse out a PDF file, which is where this error is occurring, however it works in eclipse and with other images that are on disk that aren't a java resource.
I've read other topics on the problem but nothing really seemed to help.
EDIT: addressing something in the comments, I require a String variable the library I am using requires a string input to read the image;
import org.pdfclown.documents.contents.entities.Image;
Image image = Image.get(logo);

Based on the JavaDocs for org.pdfclown.documents.contents.entities.Image I "guess" the Image.get(String) is forwarding the call to Image.get(File) using the String as the parameter for the constructor of File
This isn't going to work for URL based paths. Instead, you need to look towards Image.get(IInputStream) (why these APIs can't simply use what's already available :P)
So, digging through the API some more IInputStream leads to org.pdfclown.bytes.Buffer, not perfect, but it's a link.
You can use Class#getStreamAsResource and write this into a ByteArrayOutputStream which can then give you a byte[], which can then pass to Image.get(IInputStream)

Related

How do I play mp3 audio(s) that is stored inside the java project?

Most of the solutions tells me use the File Class, but I am planning to use the audio stored in the Java Project. If I make an .exe file, would that work when I'm using File Class?
If you are using JavaFX, there is direct support for MP3. I just discovered this page, and haven't tried using it yet. I've always used javax.sound.sampled.SourceDataLine for audio output, and added libraries as needed to deal with the compression format. There are some useful libraries on github: https://github.com/pdudits/soundlibs. Of these, I've only used the jorbis library for ogg/vorbis encoded wav files. But the mp3 decoders here have been around for a long time, have been used in countless projects, and should work.
As far as packaging audio resources, a key thing to remember is that file systems don't "see into" jar files. So, a File address is basically useless as long as the resource is packed into a jar. But a URL can specify a file that is jarred. The usual practice is to have a "resource" folder for the project that can be specified by a relative address, and to load the resource using its URL. The URL can be obtained using the .getResource method of Class
For example, if you have a class named "AudioHandler" in your project in a package loction "com.dory.mymediaplayer", and a sub folder "/res", and an mp3 file "audiocue01.mp3" in /res, the line to obtain the URL for the mp3 file would be as follows:
URL url = AudioHandler.getClass().getResource("res/audiocue01.mp3");
However, depending on the needs of the library used for decoding the mp3, you might need to use the .getResourceAsStream method. The getResourceAsStream method returns an InputStream instead of a URL.

Get RandomAccessFile from JAR archive

Summary:
I have a program I want to ship as a single jar file.
It depends on three big resource files (700MB each) in a binary format. The file content can easily be accessed via indexing, my parser therefore reads these files as RandomAccessFile-objects.
So my goal is to access resource files from a jar via File objects.
My problem:
When accessing the resource files from my file system, there is no issue, but I aim to pack them into the jar file of the program, so the user does not need to handle these files themselves.
The only way I found so far to access a file packed in a jar is via InputStream (generated by class.getResourceAsStream()), which is totally useless for my application as it would be much too slow reading these files from start to end instead of using RandomAccessFile.
Copying the file content into a file, reading it and deleting it in runtime is no option eigher for the same reason.
Can someone confirm that there is no way to achieve my goal or provide a solution (or a hint so I can work it out myself)?
What I found so far:
I found this answer and if I understand the answer it says that there is no way to solve my problem:
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
I think I can follow the reasoning behind it, but it is over eight years old now and while I am not very educated when it comes to file systems and archives, I know that the Java language has evolved quite much since then, so maybe there is hope? :)
Probably useless background information:
The files are genomes in the 2bit format and I use the TwoBitParser from biojava via the wrapper class TwoBitFacade?. The Javadocs can be found here and here.
Resources are not files, and they live in a JAR file, which is not a random access medium.

Issue loading style sheet in JavaFX

I am developing an application on Linux using OpenJDK 8u20 and OpenJFX 8u5. I am basically trying to set a default style sheet for all scenes. There apparently isn't a a sanctioned way to do that, so the work around is to set the style sheet for each individual scene. The way to do this is "scene.getStylesheets().add(css)" where css is a String representing the location of the style sheet. That representation can be in three flavors: file, URL, or resource.
An example of the URL approach is:
String css = "http://localhost/file.css";
An example of the file approach is:
String css = "file://" + new File("file.css").getAbsolutePath().replace("\\", "/");
An example of the resource is:
String css = this.getClass().getResource("file.css").toString();
Of the three, only the URL approach appears to work as advertised.
The file approach appears to work initally, but subsequent compiles appears to break it. I can see that "scene.getStylesheets().add(css)" is being called with the correct value, but the application runs as if it never was. It only works after the compile that I edit the file that I am adding the stylesheet. If I edit any other file, compile, and run it does not work.
The resource approach just throws a runtime exception, namely "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)".
I have a work around for now by using the URL approach, but long term I would really like to be able to package that CSS file in the jar and use it from the jar. Does anybody have any ideas or suggestions?
OK, apparently I didn't understand how the resource approach is supposed to work sufficiently. I compile the class files in a temporary directory, and I was not also copying the resource files to that directory. Once I did that getResource starting working. So now both the URL and resource approaches are working for me.
If anybody wants to comment on the odd behavior of the file approach, it would be nice to know what's up with that.
There is a way of setting a default CSS style sheet for all of your scenes and you can accomplish that by invoking the static setUserAgentStyleSheet() method on the JavaFx Application.

Android: .java files readable from .apk file?

I'm currently developing an application for a company which includes livescoring. The XML-files I access (from the net like: "http://company.com/files/xml/livescoring.xml") are not intended to be public and should only known to me.
I was wondering if it is possible for anyone to decode the .apk file and read my original .java files (which include the link to the XML files).
So, I renamed the .apk file to .zip and could access the "classes.dex", which seemed to include the .java files (or classes). Googling led me to a tool named "AvaBoxV2" which decoded this "classes.dex" file. Now I have a folder including an "out" folder where files named .smali exist. I opend one of these with an editor and finally there is the link to the xml file. Not good. :(
Is there a way to encrypt my app or the classes.dex file? I don't want to tell that company, that anyone can access the original xml-files. Maybe signing the app probably helps?
Also, do you know a really noob-friendly tutorial to prepare apps (signing, versioning,...) for Google Market?
Thanks in advance!
The .java source code is not included in the APK.
It is possible to disassemble the Dalvik bytecode into bytecode mnemonics using a tool like baksmali, but there's no way a user can recover the original .java source.
Furthermore, you can use a tool like proguard (included in the Android SDK) to obfuscate your byte code, making it hard to interpret the behavior of the disassembled bytecode.
You can make small tricks too, like storing the link string in some sort of obfuscated form, and then de-obfuscating it at run-time in your app (a simple example would be to use base 64 encoding, but someone could probably reverse that quickly if they wanted to).
That said, it's pretty trivial for someone to run tcpdump and sniff the network traffic between your device and the server, and get the URL that way, so there's no way to completely prevent anyone from getting this value.
Yeah, its impossible to fully prevent something like this. Its the same on a desktop application, or any other application.
As mentioned, obfuscation will help, but people who are persistent can still get past it. Especially for something like a url like that.
One solution of making it much more tricky for hackers is to use PHP on your webserver and some sort of token system to determine if the request is coming from your app or not... That would get a bit tricky though, so I don't really suggest it.

Open and edit file in .jar with Java?

How would you go about opening an .xml file that is within a .jar and edit it?
I know that you can do...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
But how would you open the xmlData.xml, edit the file, and save it in the .jar? I would find this useful to know and don't want to edit a file outside of the .jar... and the application needs to stay running the entire time!
Thank you!
Jar files are just .zip files with different file suffix, and naming convention for contents. So use classes from under java.util.zip to read and/or write contents.
Modifying contents is not guaranteed (or even likely) to effect running system, as class loader may cache contents as it sees fit.
So it might be good to know more about what you are actually trying to achieve with this. Modifying contents of a jar on-the-fly sounds like complicated and error-prone approach...
If you app. has a GUI and you have access to a web site/server, JWS might be the answer. The JNLP API that is available to JWS apps. provides services such as the PersistenceService. Here is a small demo. of the PersistenceService.
The idea would be to check for the XML in the JWS persistence store. If it is not there, write it there, otherwise use the cached version. If it changes, write a new version to the store.
The demo. writes to the store at shut-down, and reads at start-up. But there is no reason it could not be called by a menu item, timer etc.

Categories