How would I load a .dll in a way that when the project is transferred to another computer, the application would still work perfectly without changing the .dll path?
I read some threads on this website who tried to answer that question it seems, but all of them went over my head. Please explain in very basic terms (ELI5).
Thank you.
If you know where the dll file will be located in relation to the launching point of the application (i.e. the jar file), you can get the current working directory, then put together a relative path that way.
Get the current working directory using this line:
String directory = new File(".").getCanonicalPath();
If the dll is in the same folder as the jar then you can locate it like this:
File dllPath = new File (directory+File.separator+"example.dll");
Related
I'm trying to develop a cross-platform application that works on Desktop and Android as well using JavaFX and Gluon.
At runtime my code creates a serialized file in my resource folder. I also need to read and write serialized data from/to it.
I managed to work it on desktop, but not on android. Because it have a different file structure I guess.
That is why I try to get the file path dynamically.
Existing resource files, which are created before runtime (and not modified) seems to works fine on both platform.
I tried with new File("src/main/resources/folder/file.ser").getAbsolutePath(); and by trying to access it from my root folder like this: getClass.getResources("/folder/file.ser").getPath();. Both of them works fine on desktop (Windows) but unfortunately Android does not find the file by file path.
An other problem could be that I should not create runtime files in the resource folder but then where should I?
Any idea how can I read and write runtime created files that works both on android and desktop?
(If the information is not enough to help me, I try to reproduce my code in a minimal form and provide further details.)
I think you are on a completely wrong track. Creating or writing to files in the resource folder does not work in general. The idea is that files in the resource folder get packaged into jar files or are otherwise bundled with an application and are not writable at runtime.
What you should do is to create an application folder when your program is launched for the first time. A common practice on desktop is for example to create an invisible folder ".myApp" in the users home directory. On other platforms like Android there are other platform specific naming and location rules, but the concept is the same. At first launch time you can also copy necessary resources from your resource folder into this application folder so that you can edit them at runtime.
Resource files with a path on the class path, could be packed in a jar and should be considered read-only, especially as resources might be cached in some cases. They are not File. They can be captured by URL, URI, Path. The paths are case-sensitive and the path separator is /.
Hence resources can only be used as a template, an initial file. They should be copied to a real File, outside the application.
Path path = Paths.get(System.getProperty("user.home"), ".myapp/file.ser");
Files.createDirectories(path.getParent());
if (Files.exists(path)) {
URL url = MyClass.class.getResource("/folder/file.ser");
Path template = Paths.get(url.toURI());
Files.copy(template, path);
}
Furthermore .ser, a serialized java object, is not a good idea. I would suggest XML
using JAXB with annotations. More readable, maintainable, versionable. No clash between development JRE at your place and deployed JRE at the client.
I am having trouble loading Images contained within a JAR file. I have read a number of other posts related to this. But cannot find the answer. Now here is where it gets complicated.. This all works fine if I am using a Runnable JAR file exported from Eclipse and Run it using the standard JRE. However This JAR file is actually a plugin for a piece of software called pro/ENGINEER which has it's own JRE that is used to run the JAR file. The strange thing is.. that this works fine on the development machine, that has eclipse installed and so on, but doesn't work on any of the client machines?! But I can't see what is different.
I am trying to load the image using:
ImageIcon icon = new ImageIcon(this.getClass().getResource("resources/Header.png");
This method is called from my 'Start' class, and the Jar is structured as follows:
load
Start.java
load.resources
Header.png
If I Open the Jar file with WinRar you can see the Image definitely exists in the jar, in this position.
I know this may be a very specific question but if anyone can be of any help that would be great.
I did not have the particular problem you're having now, however I ran into the problem of not finding a resource in a JAR file several times in the past.
The solution was to not get the resource from the class, but from the class loader as demonstrated below:
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/Header.png"));
Mind the .getClassLoader() call after the getClass().
Hope this helps.
This question already has answers here:
I'm trying to export a runnable jar in Eclipse, but I need it to include some other files necessary for running my program
(3 answers)
Closed 8 years ago.
When I run my program from eclipse, it shows up fine, the images from Resources\ show up, as do the sounds from the same place, and the text files are found properly. However, when I export my jar, and copy the resources into it with 7Zip, the images will work, but the sounds and the text files can't be found, even though they're in the same folder, with the same path used to find them in my code. I can fix this by putting a folder next to the jar file named Resources, and put everything in there, but I'd like to know why just putting it in the jar file only worked for the images, and how I can get it to work with the text and audio files as well.
An example to show you what I mean:
File inventory = new File("Resources/inv.txt");
threadpath = "Resources/threads.wav";
enemy1 = new Sprite(new Texture("Resources/miniForestGolem.png"));
When I run it in eclipse, all three work fine, but when I export it, and put the resources folder in the jar file, only the image works.
Edit:
I know how to include my resources, and have done so, I'm asking about how/why some of the resources aren't able to be accessed, even after adding them in.
Ok, from your comments we can infer the difference between executing it from eclipse and executing it from a .jar.
From eclipse: it works, because all that new File(...) find an actual file in Resources/
From the .jar: it won't work, since there is no file in a relative ./Resources/ path from the execution path of the application.
The way to make it work is the next:
Make sure Eclipse recognizes Resources/ as a source folder (right-click on project properties, Java Build Path, and add it as a source path)
Look for a replacement for your API methods that, instead of File objects, use InputStreams. Once you have it, retrieve all your resources as InputStreams taken from the classpath. If you are inside MyClass.java, do this: MyClass.class.getClassLoader().getResourceAsStream("Resources/inv.txt"), etc.
What you have achieved by doing this: instead of File objects built on actual operating system files, you will be retrieving InputStreams read straight from your java application classpath. This way, you can package them into a jar, or into a WEB-INF/classes directory inside a web application, or a library folder in some application servers... wherever you like as long as it is into the application classpath. I would do this if I had to package your application in a portable and usable way.
I'm working on a project where I open a file, overwrite it, and save it as a new file. However, I'm having some difficulties with accessing the template file.
Right now, I believe my program is referencing the file using the path from my computer, however, if I were to export this program to a different computer, it probably couldn't find the file.
I have the file stored in a source folder for the project in eclipse. Is there a correct way to reference the file so that it will be able to be found on any computer?
I've attached an image on how my program is now referencing the file.
If the file is in the project folder, you don't need to specify a path at all. Simply using the filename "file.txt" can suffice where you would have normally put a path.
One alternative can be if you are using Tomcat server for deployment , then place this file in web-apps location of tomcat.
Accessing will be done using below code snippet
String filePath = System.getProperty("catalina.base") + "/web-apps";
This filePath will work on all computers because we set catalina base in all machines installing tomcat.
I am writing a program which is dependent on saving to a resource folder that is exported with the jar. I have a source folder titled "resources/inputs" and it exports correctly. I can load from it which is great, but the problem is, when I use:
getClass().getResource(path);
I cannot save to the path returned. I need to be able to save to it and I was wondering, is there any way I can save to this resource folder (or to some other folder existent in the same directory as the jar) no matter where the user has saved the jar file?
The error I get is a FileNotFoundException and the background I've read on it is that since jar triggers java "read-lock" you can only ever read from a jar and can never write to it? Not sure if that is accurate or not, but if it is, how can I work around this using an external folder?
I've never tried this, but its probably having a hard time because the you need to use specialized classes to access files within a jar file (which is a zip file with a different extension). Google "JarEntry" and see if that points you in the right direction.
Does this answer your question