I have a call to a HashMap's containsKey method that I expect to return true. It returns true if I compile my program to .class files and run it like that, but if I build a JAR then the same call returns false for reasons I cannot comprehend.
I've debugged both the .class and the JAR versions (the JAR using a remote connection as described in http://www.eclipsezone.com/eclipse/forums/t53459.html ) and in both cases the HashMap appears to contain the Key I'm trying to check.
The HashMap uses URI objects as keys. Here are the contents of the variables as shown in each debug session:
When run as a .class file
HashMap Key: java.net.URI = file:/E:/SSD%20App%20Libraries/Google%20Drive/Programming/Bet%20Matching/Java%20Sim/target/classes/simfiles/paytables/
URIToCheck: java.net.URI = file:///E:/SSD%20App%20Libraries/Google%20Drive/Programming/Bet%20Matching/Java%20Sim/target/classes/simfiles/paytables/
Result: GameTreeItemsMap.containsKey(URIToCheck)
is true
When run as a JAR
HashMap Key: java.net.URI = jar:file:/E:/SSD%20App%20Libraries/Google%20Drive/Programming/Bet%20Matching/Java%20Sim/out/artifacts/JavaSim_jar/Java%20Sim.jar!/simfiles/paytables/
URIToCheck: java.net.URI = jar:file:///E:/SSD%2520App%2520Libraries/Google%2520Drive/Programming/Bet%2520Matching/Java%2520Sim/out/artifacts/JavaSim_jar/Java%2520Sim.jar!/simfiles/paytables/
Result: GameTreeItemsMap.containsKey(URIToCheck)
is false
I would expect the method to return true
in both cases. Do URIs somehow behave differently inside a JAR? What's going on?
Thanks in advance for any help!
Edit 1
As was pointed out to me, the URIToCheck in the JAR case is being double encoded (%2520 instead of %20). Here's the code that generates the URIToCheck. I use the walkFileTree
method.
Files.walkFileTree(paytableHomePath, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
Path parentPath = dir.getParent();
URI parentURIToCheck = parentPath.toUri();
boolean testContain = GameTreeItemsMap.containsKey(parentURIToCheck);
return FileVisitResult.CONTINUE;
}
In the JAR case, the URI parentURIToCheck
is double encoded (with %5250 for a space where there should be %20) and in the .class case this does not happen. Any idea why?