Will InputStreamReader getResourceAsStream work in Linux? - linux

I have this code in a Java EE Application for reading the Properties file.
Even though the Myservice.properties is placed under WEB-INF/classes folder, the properties aren't being read in Linux environment, but it is working fine in Windows environment.
InputStreamReader fMainProp = new InputStreamReader(this.getClass().getResourceAsStream("/Myservice.properties"));
Will the above will only work in windows?
MyWeb() {
prop = new Properties();
try {
InputStreamReader fMainProp = new InputStreamReader(this.getClass().getResourceAsStream("/Myservice.properties"));
prop.load(fMainProp);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}

Whether that code works depends on if the classloader which loaded the calling class as represented by getClass() in your code has access to the /WEB-INF/classes. Apparently the class in question is by itself not inside the /WEB-INF/classes or has a copy which is placed elsewhere in the classpath and server make/version used in the Linux environment uses a somewhat different classloader hierarchy than the server make/version used in the Windows environment.
Fact is, if you can't guarantee that the properties file is to be loaded by the same classloader as the calling class, then you should not try to get it by the classloader of the calling class, but by the context class loader of the current thread. It has access to everything.
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("Myservice.properties"));
Please note that with this class loader, the path cannot be relative. So don't start with a leading slash.

Related

C# | Catch exception from a dynamically loaded dll

I'm trying to write a C# (WPF - not that it matters in that context) application that loads a DLL dynamically using this code:
private Assembly GetAssembly(string assemblyPath)
{
AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
return AppDomain.CurrentDomain.Load(assemblyName);
}
Loading the dll works fine and also using it works.
The problem is - when I try catching an exception when calling a function on that dynamically loaded DLL it is not caught in my application
Note: The dynamically loaded dll was also written by me so I know for a fact that an exception occurs
The try/catch section looks like that
try
{
result = aseInstance.Compile();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
I also tried using the following code:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
but didn't help..
One more thing that might be worth mentioning - useInstance is a dynamic
I don't want my code to have to "know" which class it is loading so I'm using dynamic
If i do the same using references everything works and the exception is caught
I'd love some help here!
Thanks

Rome 0.9 does not work correctly when module classloader order : parent last

Project description:
WebSphere Application Server 7.Maven project which uses Rome0.9.
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>0.9</version>
</dependency>
I was solving the problem with log4j not logging. The problem was that log4j.properties were already set in parent project.
That's why I changed module's classloader order to Parent Last.
It fixed the problem with log4j, but application now throws following exception:
ParsingFeedException: Invalid XML
I've checked parent loaded libraries and they include the same version of Rome - 0.9.
It seems that I'm missing some dependencies in my project. I wonder if there is some way to find out which libraries are missing?
Maybe you could suggest any other solution?
I don't have a solution for searching missing loaded library.
However this workaround worked for me:
I reconfigured log4j in static block of main servlet class.
static Logger logger = LoggerFactory.getLogger(FeedAggregatorServlet.class);
static {
Properties p = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
p.load(classLoader.getResourceAsStream("/FeedAggregatorlog4j.properties"));
} catch (IOException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(p);
}

Java getResourceAsStream cannot load resource

I am trying to load a class whose name is specified in a properties file. Here is the code for the same.
try {
Properties properties = new Properties();
InputStream in = MyAbstractFactory.class.getResourceAsStream("/some.properties");
properties.load(in);
String impl = properties.getProperty("key");
MyAbstractFactory factories = (MyAbstractFactory) Class.forName( impl ).newInstance();
return factories;
} catch (Exception e) {
return new DefaultFactoriesImpl();
}
This code is part of a jar file. the properties file is just outside the jar. Its unable to load the properties file and is loading DefaultFactoriesImpl instead. I know this happens when MyAbstractFactory.class.getResourceAsStream cant find the resource in the class path but that doesn't seem to be the case here.
Dir Structure:-
com
myjar.jar
some.properties
Command i am executing is "java -jar myjar.jar"
Any feedback on why this might be happening. Could this have something to do with Clasloaders? I'd like to add that when i run this code from within eclipse it seems pick up some.properties just fine.
Remove the leading slash from the argument you pass to getResourceAsStream().
Put the folder outside the JAR into the CLASSPATH when you execute the JAR. I don't know if the manifest CLASSPATH overrides the one you might pass using -cp. Play with it; one of them will work.
It's not finding your .properties file because it's not in the JVM CLASSPATH. When you do it properly, the JVM will find it.

Multiple Threads in Xpages

I have a problem and hoped somone could help me
I'm trying to start multiple threads from an XAgent (not rendered XPage)
public class ImportThread extends NotesThread {
Session currentSession;
public ImportThread(String maildb, String Server)
{
try{
currentSession =DominoAccess.getCurrentSession();
this.maildb = currentSession.getDatabase(Server, maildb);
}catch (Exception e) {
e.printStackTrace();
}
}
public void runNotes()
{
View v = maildb.getView("$Calendar");
}
in this version I could not access the View I only get "null" back
Ive tryed a version with Java Threads not realy better.
thean i've found something on Openntf
http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=Threads%20and%20Jobs
but there I got an "AccessControl Exception"
I have no more ideas, I hope that someone has an idea how to create
an XAgent with multiple thread
As Egor wrote you need the change the Java policy file if you run the Java code from an NSF. You don't have to do this if you deploy your Java code as OSGi plugin. See the documentation of that OpenNTF project.
Afaik NotesObjects should not be shared between threads. So instead of using Database mailDB you should use String mailDBName and instantiate all NotesObjects inside their own thread. You also need to watch run time: if your XAgent waits for the treads to conclude, you should be fine, but if it is a 'fire-and-forget' approach you need to start it from something more persistent like a managed bean in the session scope.
Hope that helps

Supporting multiple versions without separate builds in JavaME

I want to be able to support multiple versions of Java ME without having to have multiple builds. I already know how to detect the profile/configuration/supported JSRs. My problem is that knowing whether the JSR is supported at run time doesn't allow me to use all the features as Java-ME does not provide support for reflection.
For if I call a function added in a later version anywhere in the code - even a location that will never be run, then this could cause an error during resolution on some JVMs. Is there any way round this?
Related Questions
Handling optional APIs in J2ME
If you only need to access the class C through an interface which you know you will have access to, then it is simple enough:
MyInterface provider=null;
try{
Class myClass= Class.forName("sysPackage.C");
provider = (MyInterface)(myClass.newInstance());
}catch(Exception ex){
}
if(provide!=null){
//Use provider
}
If C does not have an interface that can be used, then we can instead create a wrapper class S that will be a member of the interface instead.
class S implements MyInterface{
static {
try {
Class.forName("sysPackage.C");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void forceExceptionIfUnavailable() {}
//TODO: Methods that use C. Class C will only be used within this class
}
S has a static block so that an exception is thrown during class resolution if C is unavailable. Immediately after loading the class, we call forceExceptionIfUnavailable to make sure that the static block is run immediately. If it doesn't crash, then we can use the methods in S to indirectly use class C.
Alternatively, we can use the method here:
Basically, you create a new package P, with a public abstract class A and a concrete subclass S private to the package. A has a static method getS that returns an instance of S or null if an exception is thrown during instantiation. Each instance of S has an instance of C so it will fail to instantiate when C is unavailable - otherwise it will succeed. This method seems to be a bit safer as S (and hence all the C APIs) are package private.

Resources