I created a jar file which use a Meta-Inf/config.properties file!!
the problem is that I would like to use Meta-Inf/config.properties from resource file and not from jar file
I tried this
InputStream input = classLoader.getResourceAsStream("META-INF/config.properties");
but this line read the config.properties file from jar and not from resource file!!
I tried also something like:
InputStream input = classLoader.getResourceAsStream("classpath:META-INF/config.properties");
but nothing
any idea
resource file: is only the directory src/main/resources
Ways of Accessing resources :
String pathToPropertyFile = "resources/META-INF/config.properties";
InputStream stream= ClassName.class.getResourceAsStream(pathToImage );
Or
String pathToPropertyFile = "resources/META-INF/config.properties";
InputStream stream= ClassName.class.getResource(pathToImage );
Assuming there is a folder by the name of META-INF under scr/main/resources/
Related
I want to get values from properties file inside JARs. I have two Jar files. both of them in class-path.
1- lib/seed.jar (has common.properties).
2- lib/span.jar (has common.properties).
Both Jars has same name of properties file but with different value
When I use the following:
InputStream input = getClass().getResourceAsStream("/common.properties");
It will read only from the first jar, but I will be unable to read value from the second jar. How to let my code be able to access the file in those jar?
This approach will be effective,use the getResources method,like:
Enumeration<URL> resources = Main.class.getClassLoader().getResources("client.xml");
while (resources.hasMoreElements()){
URL url = resources.nextElement();
File file = new File(url.getFile());
FileInputStream input = new FileInputStream(file);
System.out.println(input);
}
And pay attention to the argument of the getResources is not be necessary start with /,if not,the method will not get correct file.
I am trying to connect a keystore i created and imported some certificates into in my code.
This is code snippet
if(inputStream == null){
inputStream = getClass().getClassLoader().getResourceAsStream("resource/text.jks");
socket=sslConnect.sslSocket("00.000.000.000", 9102, "rogue1", "6000", inputStream);
}
My project is located in C:\
This is the path C:\pdsl\src\pdlsipay
Inside pdlsipay that's where i have file.java with the snippet above.
The folder resource can be found in c:\pdlsipay
Inside the folder resource is where you will find text.jks
How can i read the text.jks file in this line
getResourceAsStream("resource/text.jks");
getResourceAsStream is specifically for accessing resources that are on your application's classpath, so you would need to include C:\pdlsiplay in the classpath.
Alternatively, you can avoid getResourceAsStream and read the data directly:
File keystoreFile = new File("C:/pdlsipay/resource/text.jks");
FileInputStream keystoreData = new FileImportStream(keystoreFile);
socket=sslConnect.sslSocket("00.000.000.000", 9102, "rogue1", "6000", keystoreData);
Suppose I have text.txt inside a jar Project.jar. How to create a copy of text.txt and extract that copy into a specific folder in local drive?
Try using the java.util.jar API. Specifically, take a look at JarFile.getEntry() and JarFile.getInputStream(). For example:
JarFile jarFile = new JarFile(new File("Project.jar"));
ZipEntry entry = jarFile.getEntry("text.txt");
InputStream is = jarFile.getInputStream(entry);
// write InputStream to file
I need to be able to access a file stored in a compiled jar file. I have figured out how to add the file to the project, but how would I reference it in the code? How might I copy a file from the jar file to a location on the user's hard drive? I know there are dozens of ways to access a file (FileInputStream, FileReader, ect.), but I don't know how to look inside itself.
You could use something like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileFromJarFile);
If foo.txt was in the root of your JAR file, you'd use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("foo.txt");
assumes the class is in the same JAR file as the resource, I believe.
You can use getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead.
For example:
BufferedReader reader = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("foo.txt")));
You could read the contents of a JAR file using the JarFile class.
Here's an example of how you could get a specific file from a JAR file and extract it:
JarFile jar = new JarFile("foo.jar");
String file = "file.txt";
JarEntry entry = jar.getEntry(file);
InputStream input = jar.getInputStream(entry);
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[input.available()];
for (int i = 0; i != -1; i = input.read(buffer)) {
output.write(buffer, 0, i);
}
} finally {
jar.close();
input.close();
output.close();
}
Just wanted to add that if we want to access file inside Jar that is located at the following path(only examples as resources loading is OS independent):
Windows:
c:\your-jar-file.jar\dir1\dir2\dir3\foo.txt
Linux:
/home/your-jar-file.jar/dir1/dir2/dir3/foo.txt
Will need to use following code(pay attention that there is NO "/"(forward-slash) character in the beginning of the path):
InputStream is = this.getClass().getClassLoader().getResourceAsStream("dir1/dir2/dir3/foo.txt");
Look at the JarFile class. Everything you need to get the InputStream of a specific entry in the jar file is there.
I'm trying to get a resource for MyBatis. The tutorial states that I will need the following in my Connection Factory:
String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
My directory structure is:
src/
com/
utils/
MyBatisConnectionFactory.java
config/
Configuration.xml
I am having troubles referencing the configuration file. I tried "config/Configuration.xml", "Configuration.xml" and "/config/Configuration.xml".
Anyone have a good idea for what to do?
You can add your config directory as a source-folder (right-click > build path > use as source folder).
Thus your configuration files will go on the root of the classpath and will be accessible via getClass().getResourceAsStream("/Configuration.xml")
Open up the file via the classpath using getResourcesAsStream() rather than Resources.getResourceAsReader() For example:
InputStream is = getClass().getClassLoader().getResourceAsStream(
"src/com/utils/Configuration.xml");
byte[] data = new byte[is.available()];
is.read(data);
is.close();
String fileContents = new String(data);