Cannot find CatalogManager.properties - java

My servlet app uses XML catalogs.
First I used org.apache.xml.resolver.tools.CatalogResolver.
It finds its configuration file CatalogManager.properties under WEB-INF/classes/.
Then I tried the same thing with com.sun.org.apache.xml.internal.resolver.CatalogManager, the version which comes with the JDK.
It doesn’t work:
Cannot find CatalogManager.properties
The spec says that this file must be somewhere on the CLASSPATH, and I suppose it is.
What should I do?

Actually, it should work, the code is the same, just repackaged:
propertyFileURI = CatalogManager.class.getResource("/"+propertyFile);
InputStream in =
CatalogManager.class.getResourceAsStream("/"+propertyFile);
if (in==null) {
if (!ignoreMissingProperties) {
System.err.println("Cannot find "+propertyFile);
// there's no reason to give this warning more than once
ignoreMissingProperties = true;
}
return;
}
What to do? Try debugging, set the breakpoint und see why it does not work.
Why do you need CatalogManager.properties anyway? If you don't, you could disable the error message with the system property xml.catalog.ignoreMissing.

Related

Tomcat webclassloader fails to find a class

In a Tomcat 7 I have a pretty standar jar file on WEB-INF/lib. Inside this jar I have this class called Parser, and next to it (on the same dir) I have another one called AutomaticLocalLoader. Compilation gives no problem at all. In run time the AutomaticLoader class is found, and when It needs the Parser class, I get a NoClassDefFoundError
The Parser and AutomaticLoader class have been working without this problem for 15 years!! in many diferent vers of java and tomact; and now out of the blue, I am getting this NoClassDefFoundError, only for the Parser class. I already put a copy on a directory inside the WEB-INF/classes path and still got the same error. I already created my own ClassLoader to see if I get some error loading the class from the WEB-INF/classes directory by myself, but I can load it without problems.
log.info("Leer " + aFlInstructions[i].getAbsolutePath());
LoaderTest A = new LoaderTest();
A.test("com.hds.resolve.model.aguila.AutomaticLocalLoader");
LoaderTest B = new LoaderTest();
B.test("com.hds.resolve.model.aguila.Parser");
if(!bOverrideInputDir)
Psr = new Parser(aFlInstructions[i]);
else
Psr = new Parser(aFlInstructions[i], new String[] { StrLocalDirectory } );
The LoaderTest class, try to create the Class Object for the given name using Class.forName. If NoClassDefFoundError, then try to load the class using my own classloader and then create the class.
For the AutomaticLoader, it succed at the first try. For the Parser class if fails, then successfully load it with the custom classloader. Of course when the code reach the "new Parser" part, the old webclassloader still fails and throws the NoClassDefFoundError.
Both Parser and AutomaticLocalLoader belong to the same package and are stored on the same jar inside WEB-LIB.
Funny enough, the error does always happen on production... but never in my machine. I do not use customs classloaders except for doing this debug. Also, trying an old version of the software seems to fix the error. No idea why.
I think I can hack a solution messing with the tomcat's webclassloader, but I really would prefer to understand what is going wrong with this code.

Using corn-cps to scan for config files matching wildcard pattern

I'm trying to scan my classpath for config files matching a certain pattern. I'm using corn-cps.
The file I'm looking for is packaged in a jar and I can find it using java's default
MyClass.class.getClassLoader().getResource("jmulticonfig.3.properties")
returns
jar:file:/tmp/testjar.jar!/jmulticonfig.3.properties
I would like to find all jmulticonfig.*.properties so my corn-cps code is
List<URL> resources = CPScanner.scanResources(
new ResourceFilter()
.packageName("*")
.resourceName("jmulticonfig.*.properties")
);
An empty List is returned when I run this.
Anyone with corn-cps experience can help or suggest some other way?
Edit: To take the good suggestion of #approxiblue, the code can be found at https://github.com/kanesee/jmulticonfig.
Please make sure to add src/main/resources/jmulticonfig-3.jar to your classpath. It contains jmulticonfig.3.properties, the file which I'm trying to read using corn-cps
I think you've found some kind of bug in the corn-cps library because if you debug their classes running your code, it seems that there is a point where the resource name gets lost.
Anyway the following JmultiConfig.getConfig() implementation finds all the resources you're searching with a regular expression:
public static synchronized Properties getConfig() {
System.out.println("---");
if (s_props == null) {
List<URL> resources = CPScanner.scanResources(new ResourceFilter().archiveName("*"));
for (URL resource : resources) {
if (resource.getFile().matches(".*jmulticonfig\\..*\\.properties$")) {
System.out.println("*** -> " + resource);
}
}
}
return s_props;
}
The regular expression can be modified to match anything you need.
You are trying to obtain resources from the default package "".
But corn-cps has a bug reading resources from that package (the condition in line 63 of net.sf.corn.cps.RootedUrl fails for these resources).
For a workaround you could place your resources in another package, e.g. package res and filter for that package:
List<URL> resources = CPScanner.scanResources(
new ResourceFilter().packageName("res").resourceName("jmulticonfig.*.properties")
);
Also a filter .packageName(*) finds resources in all packages, except the default package.
The solution a Max also works, but comes at the price to construct a URL object for every resource in the classpath (except rt.jar which is not covered by corn-cps).

Catching a UnsatisfiedLinkError

I know it's not a good practice to catch errors, but in this case, it's important to do it. I'm trying to run a jar which contains a part of my game, but it's giving me an unsatisfiedlink error, but here's it's the funny part:
I'm using this code:
System.setProperty("org.lwjgl.librarypath", new File("lib/natives").getAbsolutePath());
But in netbeans, it's giving me this error, BUT, the jar runs. Now, if i change to code, to this:
System.setProperty("org.lwjgl.librarypath", new File("Dist/lib/natives").getAbsolutePath());
Now, it runs in netbeans, but the jar it's not working... and i don't get it!, so, i'm trying to use a try/catch to use either of both, and well this is the complete code:
try{
System.setProperty("org.lwjgl.librarypath", new File("lib/natives").getAbsolutePath());
}
catch (UnsatisfiedLinkError e){
System.setProperty("org.lwjgl.librarypath", new File("dist/lib/natives").getAbsolutePath());
}
and when I run it in netbeans, it's giving me the error of the first line, not an error of the catch part. What's going on? Thanks in advance friends!
In code like this:
new File("Dist/lib/natives").getAbsolutePath()
you are giving a path "Dist/lib/natives" relative to JRE "user" (or working) directory. Relative to the value of the system property "user.dir". The value of this property is set on startup of the JVM, and can vary depending on how the JVM is started. You should print or log the value of 'user.dir' and observe the difference in each startup-case mentinoed in your question to understand what is going on.
System.ou.println(System.getProperty("user.dir"));

java.lang.IllegalArgumentException: no JSON input found while trying google calendar Api in java

I downloaded google calendar api sample from http://code.google.com/p/google-api-java-client/source/browse/calendar-cmdline-sample/?repo=samples and created a project in eclipse.
Now when i try to run the project am getting java.lang.IllegalArgumentException: no JSON input found at this line
FileCredentialStore credentialStore = new FileCredentialStore(
new File(System.getProperty("user.home"), ".credentials/calendar.json"), JSON_FACTORY);
Have any of you tried this example? what is wrong here?
This error can be resolved by providing input to the .credentials/calendar.json file. If you manually provided the following entry in the calendar.json , it will work :
{
"installed": {
"client_id": "client_id",
"client_secret": "client_secret"
}
}
It seems to be the Windows problem which is not allowing to set writable permissions on calendar.json file . The method setWritable(boolean,boolean) is returning false and so is the cause of this problem. Still providing json input manually is not a perfect solutions but your application will work.
That may happen when your application executed before and it created empty .credentials/calendar.json file in you home dir. That may happen if you're running your application in Windows, cause FileCredentialStore tries to do:
file.setReadable(false, false)
and fails.
To solve it just remove calendar.json. Although you might have another error: [unable to set file permissions]
which I don't know how to solve yet.
Is that project having calendar.json resource file. Please share complete exception stack trace.
Seems some required configuration missed from calendar.json file

File.canWrite is not working as per expectations

i am trying to check whether a file is writable or not. i have changed the file permission by myself for all users. but if i try to run the program, it show "true" as a response. if i allow the permissions, then also it is showing "true".
Whats is my problem?
try
{
File file = new File("D:/myproject_log/delivery_report_edr.out");
if(!file.canWrite())
{
System.out.println("you can't write!!!");
}
else
System.out.println("you can write!!!");
}
catch(Exception e)
{
e.printStackTrace();
}
It is working fine. I copied your code and run it twice. First I got You can write, then right click on the file folder, go to properties and select read only and run the program again and I got you can't write
So as per the documentation of the method canWrite() it gave me the expected output. Please confirm your settings once again and check.
Have you tried using the java.nio.file.Files#isWritable method. as well as File#canWrite ?
I have also found that File.canWrite() cannot be trusted, especially over network drives, often returning true even though a file write will fail or vice-versa. I made my own method that actually tries to write a dummy file to the dir. That is simple to write and foolproof. Maybe they fixed it though.
I had the same issue with a file located in c:\programFiles\folder and the File.canWrite method returned true and i was getting the same exception.
when i changed the permission of write as allowed true for USER defined in security tab of Folder properties, It gave me no exception.

Categories