matlab java class path issue - java

When I put classpaths into the static class path (ie put them in the classpath.txt file) program works. When I add it dynamically, I get an error that it can't find a properties file in the class path.
Here is what I have
javaaddpath('C:\exchsys\jars\exsystools.jar')
javaaddpath('C:\exchsys\externaljars\log4j.jar')
javaaddpath('C:\exchsys\externaljars\')
javaaddpath('C:\apache-activemq-5.1.0\lib\commons-logging-1.1.jar')
javaaddpath('C:\apache-activemq-5.1.0\activemq-all-5.1.0.jar')
plus my actual code
This leads to the following error:
javax.naming.ConfigurationException: JMSUtilities.loadConfiguration(): Properties file "/exsysjms.properties" not found in the classpath.
The file it is looking for is a in the folder added through this line
javaaddpath('C:\exchsys\externaljars\')
When I put the same paths into the classpath.txt file it works fine. Any ideas?

Your experiencing issues concerning static vs dynamic classpath. A workaround for this issue is shown in this post by using ClassPathHacker.java in order to dynamically load java classes. You may check the other responses as well for further information.

Related

Java creates temp folder with shortened path and throws 'Not found' exception when trying to access files placed in it

so I did run into one very weird issue. The idea is simple: create temp dir, place some files in it and then try to access them. Now the problem is that calling File.createTempDir() or Files.createTempDirectory(prefix) creates new file inside AppData/Local/temp with shortened path, so the full path to folder looks something like C:/Users/FirstNam~1/AppData/Local/Temp/myFolder/myFile.txt instead of C:/Users/FirstName LastName/AppData/Local/Temp/myFolder.myFile.txt.
The difference is that generated path inside java contains FirstNam~1 instead of FistName SecondName. Java then throws exception File Not Found.
When I try to copy and paste shortened path into file explorer I get an error saying that file does not exist, but if I do change shortened path to full one then file opens and it works as intended.
Is there any way to fix it? Ether by forcing java to use full path names or enabling something in windows? I did Enable NTFS long paths policy, but it did not help.
This is happening when using java 8/11 and windows 10 running on VM, project is using AGP and gradle. Temp file is created inside groovy file that extends Plugin<Project>
Just when I lose hope and create a ticket, couple hours after that I find the answer. So, java has method Path.toRealPath() which solves this ~1 issue. After using this method paths no longer contain shortening and are correctly resolved.
EDIT: looks like java is doing everything correct and paths are actually valid, problem did come from library that I'm using and it's a bug with it.

Reading file form classpath

I have a maven project and want to read file in it form its class path. The code that i am using is
InputStream is = getClass().getResourceAsStream ("filename.json");
But every time i am getting null inputstreams. I am not sure why ?
The file is places under /src/main/resources. The same folder which contains log4j.xml and it is being picked up decently.
Please note, I am trying to run this file from Eclipse i.e., run or debug mode. No vm arguments or whatsoever.
The Class.getResourceAsStream(String) method looks for the given resource within the same namespace (i.e. package) that the given class is in unless you give it an absolute path (see the API documentation); If it can't find the resource on the classpath in this namespace, it returns null. Since your class is likely inside e.g. com.myproject.resourcemanagement, your resource file has to analogously be under src/main/resources/com/myproject/resourcemanagement, similary to how your class source files are organised (under src/main/java/com/myproject/resourcemanagement).

Error while load-time compiling

I created an Aspectj project and added .aj files and java files. While compiling(Load-time) it shows the error
"Error: Could not find or load main class javaagent:path/aspectjweaver-1.8.2.jar"
I compiled it in eclipse by creating Load time configuration file.
Can any one tell me why
Very simple: Probably you should provide a real file system path instead of the place holder path. Also be sure to use -javaagent:... (note the dash character) instead of something like javaagent:....

Add a new class to an existing JAR File(which contains source code)

I'll try to illustrate the problem as simple as I can.
I have a JAR file, which I extracted using Winrar. (The jar file contains an open source android library).
I want to modify this JAR file by adding a new class to the library.
So here are my steps:
First, I created a class using Eclipse and set the package name same as the android's library package name.
Second, I copied this java File to the folder of the other java files in the library.
Third, I tried to compile the JAVA file via the CMD using javac.
The path of the new java file and the other .JAVA and .CLASS files of the library is: C:\com\example\core\
The name of the new java file would be: "MyNewClass.java"
The command I run via the CMD is: javac C:\com\example\core\MyNewClass.java
But, during the compilation I get many errors saying: Cannot find symbols.
I've been looking up for a solution of this problem but couldn't figure how to solve it and make the new JAR File having another class that I created seperately.
What am I missing?
As per earlier comments:
Rather than trying to modify the JAR, you can get access to the full source code of the Universal Image Loader library by cloning the repository using git or hitting "Download ZIP" on the righthand side of the page you linked.
Once you have the source, import the library in your IDE. From there on you'll be able to build the whole thing from scratch, make any adjustments/modifications you like, etc.
Your classpath might be wrong or there might be some mistake in package name.
When a Java program is being compiled the compiler it creates a list of all the identifiers in use. If it can't find what an identifier refers to it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.
Try:
javac -cp com/* C:\com\example\core\MyNewClass.java
That should make the compiler aware of all the other classes under com/...

Where to place a file if it is getting accessed using ClassLoader.getSystemResource in WebApplication

I am using one third party jar in my code. In the jar file , in one of the classes, when I opened the class using de-compiler, the code below is written:
java.net.URL fileURL = ClassLoader.getSystemResource("SOAPConfig.xml");
Now I am using this in my webapplication, where should I place this SOAPConfig.xml so that it will find the fileURL.
Note: I have tried putting this XML in WEB-INF/classes folder. But it is not working. Your help will be appreciated.
In Addition: In the explaination you have given, It is telling me not to use this code snippet inside the third party jar in this way...What is the exact usage of this statement
ClassLoader.getSystemResource will load the resource from the system classloader, which uses the classpath of the application as started from the command line. Any classloaders created by the application at runtime (i.e. the one that looks in WEB-INF/classes) are not on the system classpath.
You need to
Look through the script that starts your server, find out which directories are on the classpath there, and put your SOAPConfig.xml in one of those. If necessary, change the classpath in the script to look in a separate directory that's just used for your config file.
Track down the person who used ClassLoader.getSystemResource in the library, kick them squarely in the nuts, and tell them never to do that again.

Categories

Resources