I am trying to consume the WSDL file in the spring boot application, WSDL file is placed into the src/main/resource/wsdl/file.wsdl.
Project structure:
Code:
static {
URL url = null;
try {
url = new URL("file:///" + System.getProperty("user.dir") + "/src/main/resources/wsdl/outbound.wsdl");
} catch(IOException e) {
java.util.logging.Logger
.getLogger(OutboundService.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}",
"file:/D:/ERPLOGIC/ERPProjects/JAVA/mavenproject/eclipse-workspace/topconpoc/src/main/resources/wsdl/outbound.wsdl");
}
WSDL_LOCATION = url;
}
It works fine in locally, but when deployed in the AWS server as a war file, it does not work.
What is the proper to refer to the path in the Spring Boot application?
Try this
URL url = Thread.currentThread().getContextClassLoader().getResource("wsdl/outbound.wsdl");
This should work
The file is placed inside the Java resources directory src/main/resource and should be loaded from there in all cases. The reason it's not loading properly when deployed is that file is not in that location once packaged. Use the class to locate the resource file from the packaged Java app. This should work both locally and when deployed. Make sure to replace ClassFileWhereCodeLives with the class the static code block is inside of.
url = ClassFileWhereCodeLives.class.getResource("/wsdl/outbound.wsdl")
Related
I am trying to get access to a folder that is created in the classpath in a Spring boot application. A snippet of the code is below:
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
URL url = classLoader.getResource("converters");
LOGGER.debug("**************** url: " + url);
File file = new File(url.toURI());
Path path = Paths.get(file.getAbsolutePath());
Arrays.stream(path.toFile().listFiles()).forEach(f -> LOGGER.debug("*******files: " + f.getName()));
if (!path.toFile().isDirectory()) {
throw new IllegalArgumentException(ErrorCode.INVALID_MAPPERS_DIRECTORY.formatMessage(path));
}
The code above runs without any issues when I run it in Intellij and I get the url as below:
file:/C:/Users/user1/projects/my-service/test/build/resources/main/converters
When I run it on Linux inside the application rpm, I get the url value below:
jar:file:/opt/home/libexec/my-service-2.0.0-SNAPSHOT.jar!/BOOT-INF/lib/my-service-api-2.0.0-SNAPSHOT.jar!/converters
Any reason why is the different behavior?
The difference is the packaging.
Your IDE does not package your application to run it, it just uses the file system as this is faster.
When you package your app and deploy all the resources that your ide can access from the file system are now packaged within your spring boot fat jar file. In your case the file is inside the my-service-api-2.0.0-SNAPSHOT.jar which is packaged inside your fat jar.
Problem
I need to be able to have a reference to a folder that a user may create in the classpath of my Spring Boot application.
Solution
The following worked for me:
mapperFilesFolder = resolver.getResources("classpath*:" + mappersLocation + "/.");
Path path = Paths.get(mappersFolder[0].getURI());
I need a way to get a resource path in Spring Boot because I will use this path as an argument to another method in existing library, which I cannot change.
It is used as:
FileInputStream fis = new FileInputStream(fileName);
I have to provide that filename there. The resource in a Spring Boot application is under src/main/resources folder. Using Resource and getResource() does not help, as I need to provide a String of a file path.
Using just "src/main/resources/filename" does not work too.
So, my question is: how do you get the path of the files that are in the Spring Boot classpath?
I have solved it this way:
try {
filename = ResourceUtils.getURL("classpath:").getPath()+ "myFileName";
} catch (FileNotFoundException e) {
e.printStackTrace();
}
With this method, when I run an application, it is working, but when I run the jar, then I get an exception:
(The filename, directory name, or volume label syntax is incorrect)
Did anyone get this issue? Please help
I'm developing a web application with sqlite database. While developing the application, I created the database connection in a java class. There I've mentioned my url as like below
String url = "jdbc:sqlite:D:\\Database\\profileDB.db";
I want to place the db file from that location to specific location inside my Webcontent folder say resources.
Now I want to change my URL pointing to folder file.
Webcontent/resources/profileDB.db
.
Anyone kindly suggest me how to do this. For the information, I'm not using any servlet in creating the connection, it is just a normal java class.
I dont know about the SQLite database but if you just want the url to the db file the you can use getResource("name_of _the resource") method of the Class class. it gives you the URL of the named resource. the rules for searching for the resource depends upon the classloader used to load the class.
for eg : my package(folder) structure is :
test-
|-foo.class
|-image.png
then inside foo.class
URL url = getClass().getResource("image.png");
it will give the url to the image.png
IMHO, you need to add sqlite.jar in Project Properties -> Java Build Path -> Libraries -> Add External Jar.
Then you can try connecting using:
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("JDBC:sqlite:sample.db");
} catch (Exception e) {
e.printStackTrace();
}
You can refer this link for sqlite jdbc
I have an application which is a ear file which contains war and some jar file. I want to access my war's web-inf folder using java code. I tried a lot but could not found anything that can help me to access my war file.
I extracted my ear file in folder structure according to jboss forums but still cant access web-inf folder.
i tried this code but it is returning the jboss bin folder not deployment folder
org.jboss.vfs.VirtualFile vFile = org.jboss.vfs.VFS.getChild("WEB-INF/config/custom.xml");
org.jboss.vfs.VirtualFile vFile1 = org.jboss.vfs.VFS.getRootVirtualFile();
I also tried using JNDI but could not solve the main problem.
InitialContext ic;
try {
ic = new InitialContext();
String moduleName = (String) ic.lookup("java:module/MyModule");
String appName = (String) ic.lookup("java:app/devcenter");
System.out.println("app name is : " + appName);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Can anyone help me? How can I access my WEB-INF folder so that I can change my xml content at runtime?
The simple answer is you can't. An EAR is an archive which means you'd have to know the path, uncompress the archive (or at least the file from the archive), change it and compress the archive again. This would likely require or trigger (can't recall if it happens automatically) a redeploy.
If you have a file you're using for configuration I would suggest you store it in a known path you add to the paths in the server configuration.
You can't access it that way. What you can do is include that in your build path. Then you can access it like you would access any resource files. So copy them over to a folder inside your source folder. For example - src\main\resources\config\custom.xml. Your java classes will be inside src\main\java\ and your resource files will be there.
In order to load external JAR files that are not placed in WEB-INF/lib folder of my web application I have written a custom loader class by extending org.apache.catalina.loader.WebappLoader class.
What it does is on startup it scans the folder (in my case it is cataline_home\plugins\) and adds all the classes/jars in this folder to the classpath of my web application.
This all is working fine, it loads all the available classes and I can even execute them from my application; but for some reason these classes can't access the properties files within the JAR's.
Say I have JAR called pluginsms.jar, inside this JAR I have a properties file called sms.properties and I am using ResourceBundle resource = ResourceBundle.getBundle("sms"); to read this file, it works perfectly fine when I run it as standalone but when I try to load it in my web application (via the custom WebappLoader) it throws: Resource Not Found Exception.
Given below is the source for my WebappLoader:
public class PluginLoader extends WebappLoader {
#Override
public void setContainer(Container container) {
StandardContext ctx = (StandardContext) container;
try {
File pluginFolder = new File(System.getProperty("catalina.home"),
"plugins");
for (File file : pluginFolder.listFiles()) {
if (file.isDirectory()) {
continue;
}
if (file.getName().endsWith(".jar")) {
addRepository(file.toURI().toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
super.setContainer(container);
}
}
What my guess is that your property file resides in WEB-INF or Webcontent folder and you are accessing you property file using relative path. While your jars who want to use this property file can't see this path that's why you are getting this exception. I would suggest you to use static path for property file. I hope that can help you.