java.io.FileNotFoundException: src/config.properties (No such file or directory) - io

Properties getprop() throws IOException {
Properties prop = new Properties();
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
return props;
}
This method is used to load the file and file is placed in classpath of project
and this method is used while getting the JDBC connection.
and after once we build application JAR is generating and that Jar is placed in MDMHubConsole and loding the URL is the procedure.
Same app is working as Just java app,But after Jar generation and replaced in MDMHUBConsole end up with NullPointerException

Related

InvalidAlgorithmParameterException

I wrote a GUi java program which brings data from db2 and shwos into a JtextArea when click a Jbutton.
I have used encription for ID/PAssword as below code
Properties properties = new Properties();
properties.setProperty("securityMechanism",
Integer.toString(DB2BaseDataSource.ENCRYPTED_USER_AND_PASSWORD_SECURITY));
properties.setProperty("user","myuserid");
properties.setProperty("password","mypassword"); myConn =
DriverManager.getConnection(url, properties);
It runs fine when i run it in eclipse.
Just created a runnable jar file using export option (selecting package reqd libs into generated jar option).
When I double click the jar file it shows the gui window but when i click the button it does not gettting any data.
I saved data base connectivity logs into a file which shows.....
Error msg: [jcc][1071][10615][4.8.86] Caught java.security.InvalidAlgorithmParameterException
while initializing EncryptionManager.
See attached Throwable for details. ERRORCODE=-4223, SQLSTATE=null
SQLSTATE: null
Error code: -4223
Please help to solve this problem.
Thanks,

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.

How to get handle to a resource within a JSF class?

I've put a properties file within src/main/resources in my JSF project.
How do I get a handle to it? I understand that EL doesn't work within a backing bean.
Note: The file is in src/main/resources - NOT src/main/webapps/resources, so the following doesn't work:
FacesContext context = FacesContext.getCurrentInstance();
File value = context.getApplication().evaluateExpressionGet(context, "#{resource['resources:email.properties']}", File.class);
It's thus in the classpath. You can just use ClassLoader#getResourceAsStream() to get an InputStream out of it. Assuming that src is the classpath root and that main/resources is the package:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("main/resources/foo.properties");
Properties properties = new Properties();
properties.load(input);
// ...
Alternatively, if it's supposed to be specific to the webapp and thus isn't supposed to be overrideable by a file on the same path elsewhere in the classpath which has a higher classloading precedence (e.g. in appserver's lib or the JRE's lib), then use ExternalContext#getResourceAsStream() instead.
InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("main/resources/foo.properties");
Properties properties = new Properties();
properties.load(input);
// ...
As to the #{resource} syntax, this is indeed specifically for CSS/JS/image resources placed in /resources folder of public web content. See also How to reference CSS / JS / image resource in Facelets template?

Will InputStreamReader getResourceAsStream work in 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.

Accessing properties file in a JSF application programmatically

I am trying to access the i18n properties file I'm using in my JSF application in code. (The idea is to have a page that displays its keys and values as a table actually.)
The project is a maven project, and in the src/resources/localization folder, and deployed in the war file in WEB-INF\classes\localization\
java.util.Properties prop = new java.util.Properties();
String path = "localization/stat_codes.properties";
InputStream foo = prop.getClass().getResourceAsStream(path);
But the variable foo turns out to be null whatever I set the path variable to, /WEB-INF/classes/localization/stat_codes.properties, "localization.stat_codes.properties" etc. A similar question is here, but there is no helpful answer there as well.
The Class#getResourceAsStream() can take a path which is relative to the location of the Class which you're using there as starting point. So, for example, if the class is located in the com.example package and you request the path foo/filename.properties, then it will actually load the com/example/foo/filename.properties file. But if you use /foo/filename.properties, then it will actually load foo/filename.properties from the classpath root.
So, your code
java.util.Properties prop = new java.util.Properties();
String path = "localization/stat_codes.properties";
InputStream foo = prop.getClass().getResourceAsStream(path);
will actually look for java/util/localization/stat_codes.properties file.
But in applications with a complex multiple classloader hierarchy, the one classloader isn't the other. The classloader which loaded the core Java classes does not necessarily have knowledge about files which are in the webapp's /WEB-INF/classes. So prefixing the path with / will not necessarily be the solution, it would still return null.
If you can guarantee that the current class is visible by the same classloader as the properties files (because they're in the same sub-root of the classpath, e.g. /WEB-INF/classes, then you should indeed use
String path = "/localization/stat_codes.properties";
InputStream foo = this.getClass().getResourceAsStream(path);
But if at some point, the properties files will be externalized because of more easy maintenance/editing during runtime so that you don't need to rebuild/redeploy/restart the webapp whenever you want to edit the files, then the above line of code will likely fail as well. The externalized location would be only accessible by a different classloader. The canonical solution is to use the thread's context classloader as starting point instead, it has access to all resources in the classpath.
String path = "localization/stat_codes.properties";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream foo = loader.getResourceAsStream(path);
(note that this one cannot take a path starting with /, it's always relative to the common root)
See also:
Where to place and how to read configuration resource files in servlet based application?
ExternalContext#getResourceAsStream() returns null, where to place the resource file?
It seems that the culprit is the prop object, I supposed any object would work, but it has to be the current object (this) on which the method getClass() is invoked, it seems. Also, the path should start with a / since the localization directory resides in WEB-INF/classes.
String path = "localization/stat_codes.properties";
InputStream foo = this.getClass().getResourceAsStream(path);

Resources