Placing java classes at the head of javaclasspath
10 views (last 30 days)
Show older comments
I have been working on a java object that needs some logging facility. I implemented this with org.slf4j.simple.SimpleLogger. Here is a test:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
static Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
String message;
if (args.length == 0) {
message = "Hello world";
} else {
message = args[0];
}
new Main(message);
}
public Main(String message) {
System.out.println(LoggerFactory.class.getResource("LoggerFactory.class"));
log.info(message);
}
}
If I call Main(), it works and reports
$ java -jar target/logtest-1.0.jar
jar:file:/home/jeffemandel/logtest/target/logtest-1.0.jar!/org/slf4j/LoggerFactory.class
2025-11-09 11:12:55.494 [INFO] org.jeffmandel.logtest.Main - Hello world
From MATLAB, calling Main reports
>> Main('test');
jar:file:/Applications/MATLAB_R2025b.app/java/jarext/slf4j/slf4j-api.jar!/org/slf4j/LoggerFactory.class
Since I don't have log4j12 configured, the output goes to NOP.
As noted in this Stackoverflow answer, if I add the full path to slf4j-api-2.0.17.jar to my javaclasspath.txt with the tag <before>, I get the expected behavior:
>> Main('Hello world');
jar:file:/Users/jeffemandel/.m2/repository/org/slf4j/slf4j-api/2.0.17/slf4j-api-2.0.17.jar!/org/slf4j/LoggerFactory.class
2025-11-09 11:24:47.349 [INFO] org.jeffmandel.logtest.Main - Hello world
Finally, if I add logtest-1.0.jar with the <before> tag, I don't need to add slf4j-api-2.0.17.jar to javaclasspath.txt
>> Main('Hello world');
jar:file:/Users/jeffemandel/MATLAB-Drive/dise_service/logtest-1.0.jar!/org/slf4j/LoggerFactory.class
2025-11-09 11:26:44.041 [INFO] org.jeffmandel.logtest.Main - Hello world
This seems to be the best approach, as it avoids cluttering up the classpath with jars I only need in my own objects.
Now I get that this is a long run for a short slide (I could just configure my object to use slf4j-log4j12), but according to the author of the SO post this undocumented feature has been around since 2013a, so maybe it should be a standard feature. I say this because on the way to understanding the issue, I saw the recommendation to edit the file that says don't edit this file, and saw someone who solved the problem by deleting slf4j-api.jar from jarext.
0 Comments
Answers (1)
Leepakshi
on 13 Nov 2025 at 7:53
Hi Jeff,
The solution you can try is to add your own SLF4J JAR (and backend) to MATLAB’s javaclasspath.txt with the <before> tag, ensuring your version is used and logging works as expected.
Thanks
0 Comments
See Also
Categories
Find more on Java Client Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!