Weblogic 10.3 cannot find external properties file - spring

I have tried DeploymentException & Class Not Found on WebLogic Admin Server 11g among other examples, but I have not been able to get Spring to read external properties file.
I have this code, which read a properties file. Putting the file (PROPERTIES_FILES) in src/main/resources, the app i deployed fine. Moving it to an external folder in the file system, it failes to deploy.
I tried http://www.mkyong.com/java/how-to-print-out-the-current-project-classpath/ to pribt the classpaths and get:
/C:/Oracle/Middleware/patch_wls1034/profiles/default/sys_manifest_classpath/weblogic_patch.jar
/C:/Oracle/Middleware/patch_oepe1040/profiles/default/sys_manifest_classpath/weblogic_patch.jar /C:/Oracle/Middleware/patch_ocp360/profiles/default/sys_manifest_classpath/weblogic_patch.jar
/C:/Oracle/Middleware/jdk160_21/lib/tools.jar
/C:/Oracle/Middleware/wlserver_10.3/server/lib/weblogic_sp.jar
/C:/Oracle/Middleware/wlserver_10.3/server/lib/weblogic.jar
/C:/Oracle/Middleware/modules/features/weblogic.server.modules_10.3.4.0.jar
/C:/Oracle/Middleware/wlserver_10.3/server/lib/webservices.jar
/C:/Oracle/Middleware/modules/org.apache.ant_1.7.1/lib/ant-all.jar
/C:/Oracle/Middleware/modules/net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar
/C:/Oracle/Middleware/wlserver_10.3/common/derby/lib/derbyclient.jar
/C:/Oracle/Middleware/wlserver_10.3/server/lib/xqrl.jar
Trying to read test.properties
Found false
Could not find properties file: test.properties
My implementation looks like this:
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
logger.debug(url.getFile());
}
logger.debug("Trying to read {}", PROPERTIES_FILES);
Resource resource = new ClassPathResource(PROPERTIES_FILES);
logger.debug("Found {}", resource.exists());
try {
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
logger.error("Could not find properties file: " + PROPERTIES_FILES, e);
}
I have the same problem to move another properties file from in servlet-dispatcher:
<context:property-placeholder location="classpath*:test.properties"/>
But I guess it's the same problem.
I am on Windows.
Can anybody help me?

Did a full system restart and it worked.

Related

SpringBoot Unable to load Property file from Runnable jar due to multiple exclamation mark

I have a Spring Boot project and everything works fine locally. Now when i create a runnable jar to run it via jenkins then it is not able to load Property file.
Following is the code where PropertyPlaceholder is configured:
#Bean
public static EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurerEncrypted() {
String env = System.getProperty("spring.profiles.active") != null ? System.getProperty("spring.profiles" +
".active") : "ci";
EncryptablePropertyPlaceholderConfigurer ppc =
new EncryptablePropertyPlaceholderConfigurer(getStandardPBEStringEncryptor());
ppc.setLocations(new ClassPathResource("application.properties"),
new ClassPathResource("application-" + env + ".properties"));
return ppc;
}
In order to debug i added following code within this:
try {
String s = new String(Files.readAllBytes(new ClassPathResource("application-" + env + ".properties").getFile().toPath()));
LOG.info(s);
} catch (IOException e) {
LOG.error("Unable to read file",e);
}
And it gives this error :
java.io.FileNotFoundException: class path resource [application-qa1.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
17:23:44 at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
I have confirmed that file is located in jar at this location BOOT-INF/classes/application-qa1.properties
So effectively issue is caused due to second exclamation mark showing up in path while loading file from jar /var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
Ideally exclamation mark should appear only after jar name.
Can someone please advise on how to address this issue.
You can't read files from a JAR like this. You have to use getResourceAsStream like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("application-" + env + ".properties");

Java I/O help. Jar Cannot Open File: URI is Not Hierarchal

I have been searching for 3 days now nonstop looking at every post I can find. My program runs on IntelliJ, but cannot run on an executable. Your help would be appreciated :)
More importantly, where can I find a in depth user-friendly tutorial? Is there a course or book I can pay for? On Udemy, the java classes completely fail to mention I/O such as classpath and URI. TutorialsPoint briefly goes over I/O buts its not indepth. Did I miss something? Is there an easier way to do all this??
Similar posts that have not worked for me:
Java Jar file: use resource errors: URI is not hierarchical
https://stackoverflow.com/a/27149287/155167
I am trying to load an excel file. I am using Maven. Apache POI says it needs a File. So InputStream does not work. http://poi.apache.org/components/spreadsheet/quick-guide.html#FileInputStream
When I java -jar jarFile, it gives me the error:
C:\Users\1010\Documents\Personal\MonsterManager>java -jar monsterManagerVer3.jar
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at java.base/java.io.File.<init>(File.java:421)
at LoadExcel.<init>(LoadExcel.java:62)
at monsterRunner.<init>(monsterRunner.java:13)
at monsterRunner.main(monsterRunner.java:24)
Here is the code
public LoadExcel() throws IOException, URISyntaxException, InvalidFormatException {
mNames = null;
URL ins = this.getClass().getResource("/excel_List.xlsx");
if (ins == null)
throw new FileNotFoundException("The XLSX file didn't exist on the classpath");
ins.toString().replace(" ","%20"); //<-Runs without this part
File file = new File(ins.toURI()); //this is line 62
// File f = new File(getClass().getResource("/MyResource").toExternalForm());
//String path = this.getClass().getClassLoader().getResource("/excel_List.xlsx").toExternalForm();
//File file = new File(path);
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
//FileInputStream fis=new FileInputStream(new File(excelFile));
// XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
loadExcel(sheet);
cacheNames();
// fis.close();
wb.close();
}
If it helps here is the path to the excel file:
src\main\resources\excel_List.xlsx
UPDATE:
so I took the excel file out of the resources folder
\nameOfMyProgram\excel_List.xlsx
and now I get this error.
I tried several versions of using the classLoader, Class and Thread to solve this error from Different ways of loading a file as an InputStream
but I still cannot get it to compile.
Error and my code
If you have to use File object do not put xls-file into resources directory.
Maven puts all files from resources directory into jar.
Java can not create File object based on file in jar-file.
Put your xls-file somewhere in file system and create File object based on its URL.
Since your xls-file is not a resource do not use getResource.
Its URL is its full filename (with path).
This code below works with jar executable
String path = new File("excel_List.xlsx").getAbsoluteFile().toString();
File file = new File(path);
if(!file.exists()){
JOptionPane.showMessageDialog(null,"File not found!");
System.exit(0);
}
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook wb = new XSSFWorkbook(pkg);

cannot load and read from properties file

My grade java project has the following structure.
As you can see the resources folder is in present in the class path.
But when I run the following in a class under java folder
new File("somefile.txt").exists()
I get FileNotFoundException.
Could anyone help me find why I am not able to access this file.
This is in the class path.
You can use.
ClassLoader classLoader = getClass().getClassLoader();
String filePath= classLoader.getResource("filename").getFile();
new File(filePath).exists();
For more info, you can go through this tutorial.
You can resolve your issue like below
Properties prop = new Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("somefile.txt");
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("Property file '" + fileName + "' not found in the classpath");
}
I found it from the post How to read properties file in Java

write file into spring boot folder

my spring boot project structure is like this
src
|-main
|--|-java
|--|-resources
static
|-css
|-images
|-js
now I want to write a file into the static/images folder
I tried to new File like
BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(new File("static/images")));it will throw "No such file or directory" exception
but in other html file I can get the js by "js/jsFile.js"
new File("static/images") is right
I used new File("/static/images") so I got an Exception
I was in the situation where using Spring Boot I had to save the image into one directory which is accessed statically.
Below code worked perfect
byte[] imageByteArray ....
String fileName = "image.png";
String fileLocation = new File("static\\images").getAbsolutePath() + "\\" + fileName;
FileOutputStream fos = new FileOutputStream(fileLocation);
fos.write(imageByteArray);
fos.close();
Hope it helped.
#zhuochen shen is correct.
The thing is to happen me is Eclipse doesn't show write file. So I looked at file explore. File is writing correctly.
try {
Path path=Paths.get("static/images/"+productDto.getImage().getOriginalFilename());
Files.write(path,productDto.getImage().getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
enter image description here
enter image description here

Maven java.io.FileNotFoundException

I'm developing a project with Maven. In a class to send e-mails, in run and dev modes, I get the following error: Caused by: java.io.FileNotFoundException: jQuery/images/logo.png (Ficheiro ou directoria inexistente) ==> translation = File or directory not found.
I've tryed lots of paths, like "./jQuery/images/logo.png", "/jQuery/images/logo.png" and others. The full relative path is: "src/main/webapp/jQuery/images/logo.png".
In "target" folder, the path is "project-1.0-SNAPSHOT/jQuery/images/logo.png".
Inside war file, is "jQuery/images/logo.png".
I don't think it's important, but I'm using NetBeans 7.1.1 as IDE.
I found that the absolute path returned in runtime is "/home/user/apache-tomcat-7.0.22/bin/jQuery/images/logo.png"!... It's not the project path!
How can I get a file in webapp folder and descendents from a Java class, in a Maven project?
The code is:
MimeBodyPart attachmentPart = null;
FileDataSource fileDataSource = null;
for (File a : attachments) {
System.out.println(a.getAbsolutePath());
attachmentPart = new MimeBodyPart();
fileDataSource = new FileDataSource(a) {
#Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());
multipart.addBodyPart(attachmentPart);
}
msg.setContent(multipart);
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, from, "password");
transport.sendMessage(msg, msg.getAllRecipients());
The path .../apache-tomcat-7.0.22/bin/jQuery/... is really odd. bin contains scripts for Tomcat, it should not contain any code nor any resources related to your application. Even if you deploy your app in the context /bin, the resources would end up under `.../apache-tomcat-7.0.22/webapps/bin/...
This looks like a mistake made in an earlier deployment.
Now back to your question. To get the path of resource of your web app, use this code:
String absolutePath = getServletContext().getRealPath("/jQuery/images/logo.png");
(docs)

Resources