MATLAB: importing java libraries - java

I have the following lines in my MATLAB code:
javaaddpath('C:\Users\localadmin\SkyDrive\IdeaProjects\DrawModels\out\artifacts\DrawModels_jar\DrawModels.jar')
javaaddpath('C:\Users\localadmin\SkyDrive\IdeaProjects\DrawModels\out\artifacts\DrawModels_jar\itextpdf-5.4.1.jar')
I then go on to instantiate some JAVA objects defined in the files.
I would like to copy the JAR files to where the .m file sits, and then write the following instead (i.e. I want to use a relative path rather than an absolute one).
javaaddpath('DrawModels.jar')
javaaddpath('itextpdf-5.4.1.jar')
However, the MATLAB path doesn't seem to apply to javaaddpath and so the JAR files do not get loaded.
Any ideas as to how I may achieve a relative path addressing of JAR files?
The background for this is I want the MATLAB code to run both on Windows and Linux, i.e. whereever it is unpacked. The user may unpack it in any folder he chooses, so I don't want a hard-coded path in the source.
Any help appreciated.

You can use mfilename to find the path of the running M file, which you can concatenate with the JAR file name like,
javaaddpath(fullfile(fileparts(mfilename('fullpath')),'DrawModels.jar'))
That must go in the M file with which you would like to add the Java class to MATLAB.

Related

Create directories and files by given path with jar

I have a java maven project, which converts .json files to .yaml and vice versa. Long story short,I wrote method which creates resulting directory of converting process using path of where jar actually is. Then I created the method which creates files and writes there result of converting process to the /path_of_resulting_dir/file-name.json/.yaml. In case I run jar from my project in IntelliJ idea - everything is ok. However, when I run jar file from random folder on my desktop, nothing happens.
So, how can I correctly make jar to know if there are any files I should convert placed next to the jar file(e.g. C:\path_to_jar\SomeJarDir\jar.jar So files are in the SomeJarDir with a jar), or if there are any files which I access from path I write as jar argument need to be converted.
Link to the GitHub, where is my code placed(old version where I haven’t tried to make jar to know what to convert from any folder it’s placed, which works only if u run jar in IntelliJ idea):
https://github.com/foreverdumb/javaSprSum2021/tree/JavaSprSmrHmwrk/michaelProject
P.S. How can I force log4j to create log file next to jar?
your program maybe using relative path, so when you run your program from different location, it will read the directory relative to itself. Either provide the path during runtime as user input or set the absolute path to the jar.

Where should i place my files in order to be able to access them when i run the jar?

I store some objects in files and everything works fine in Netbeans console. But when i run the JAR through the command line (java -jar myapp.jar) i get this:
Where should i place the files in order to be able run the app through the command line?
The answer will depend on if you want to write to the files or not...
You could...
Ensure that the files are placed relative to the Jar file and use a relative path. The problem with this is if the execution context is not the same directory as where the jar and files are stored, you won't be able to find them again...
This will also be dependent on your build process to make sure that any required files are placed copied to the build location of the Jar
You could...
Place the files within a well know location, for example, on Windows you could use {user.home}/AppData/Local/{application name} or on Mac you could use {user.home}/Library/Application Support/{application name} and place the files here, then you could use an absolute path to the files
This likely becomes an installation issue, as you need to ensure that any required files are copied to the required location when the application is installed.
If the files are auto generated at runtime, then you just need to make sure the directories exists and make them if they don't
You could...
Store the files within the Jar context (AKA embedded resources), the means by which you do this will depend on your IDE and build process, for example, in Netbeans, you can copy files into the src directory of your project and they will automatically be included in the resulting Jar file. I believe that Eclipse can work the same way. However, if you're using Maven, you will need to place the files into the resources directory instead.
You would then access these resources using Class#getResource or Class#getResourceAsStream depending on your needs.
This will make the resources read-only however.

How to distribute native libraries with jar?

This is the directory structure I want to create when I finally deploy my software. It is a Java chat client with a webcam feature and for the webcam I am using LTI-CIVIL.
I was told that I can not use DLLs right from the JAR and I will have to extract them somewhere. All cool. However, what I cannot get my head around is how can I make it work ?
LTI comes with a large number of files in the zip that they provide on their site. If you are using Eclipse, you need to set the path to appropriate folder for the native library. However, this limits me to Eclipse and prevents me from distributing the JAR to my friends. Apparently, I will now have to point to that folder, and maybe load the files, programatiaclly
I am a beginner so if someone can download LTI-CIVIL, have a look at the directory structure and let me know how to achieve what I am trying to do then that would be highly appreciated.
AFAIK, for my 32 bit Windows, I need to point to native/win32-x86 folder.
What I am trying to do is to load the appropriate files in memory so that I can provide webcam facility. I want to avoid installers and simply give a zip file with a directory structure mentioned above so that people can extract, run the jar file from the folder and start chatting.
Clarification: I am trying to send a library with jar file and not in jar. I know extracting and using dlls from jar is tough
I'm assuming that it is not your own code which loads the native libraries (System.load), and they are loaded by a third-party jar (lti-civil).
In this case you have to set the enviroment variable LD_LIBRARY_PATH appropiately before lti-civil attempts to load the native libraries.
Either:
With a launcher script (e.g .bat), set the variable before running java, or set the system property, something like:
java -jar your.jar -Djava.library.path=/path/to/native/folder
At runtime. In the entry point of your program.
This is a bit "hackish", but it works.
Check this link for example:
http://nicklothian.com/blog/2008/11/19/modify-javalibrarypath-at-runtime/
Since you do not know the exact path beforehand, in both cases you will have to also find the correct path where the native libraries are located.
If the path to the libraries is relative to the path of the jar/launcher, then find the current path of the executable:
in a .bat launcher:
Get Directory Path of an executing Batch file
in java:
How to get the path of a running JAR file?
And then that, you can assume the libraries are located in path relative to this (../native), just calculate the path (and maybe expand it to an absolute path).
After you have calculated the absolute path, set the enviroment/system property as described in the first part of the answer.

Converting Java URL to valid File path in Linux

Ok I'm developing in Linux using Eclipse a program that needs to read a text file. The idea is to have the JAR and text file on the same folder. So I'm getting the text file path like this:
Client.class.getClassLoader().getResource("Client.class");
This correctly returns the path and I append the file name and get the below path:
/home/marquinio/workspace/my_project/info.txt
Problem is when I export my project into an executable JAR file. The JAR can't read the file. I double checked and everything looks fine. The only problem I see is that now the path has some "file:" appended at the beginning like this:
file:/home/marquinio/workspace/my_project/info.txt
which is probably why I'm getting a "FileNotFoundException". The JAR and text file are both in the same folder.
Anyone knows how to fix this? Why would Java behave different between Eclipse and executing JAR in command prompt?
Is there a replacement to the "...getResource(...)" provided by Java without that "file:"?
NOTE: this JAR should also be compatible in Windows environment. Still need to test it.
Thanks in advance.
The resource you are referring to is not guaranteed to be a file on the file system. Why not use ClassLoader#getResourceAsStream()? Without looking into the details, my best guess is that the different behavior you are seeing is because a different classloader is being used in each case above.

How to Load File Outside of, but Relative to, the JAR?

I need to load a file outside of the JAR, but relative to it (lib/config/config.ini to be exact). I used that exact path, and it works fine as long as the working directory is where the JAR is, i.e.
/path/to/jar$ java -jar JAR.jar
If it's run like this:
~$ java -jar /path/to/jar/JAR.jar
It can't find it. How can I correctly load a file relative to the location of the JAR?
Try using getClassLoader().getResource("classname") to find the URL of a class in your jar file. You'll find it's delimited with a ! between the path to the jar, and the path within a jar, which you can easily slice.

Categories

Resources