I have a schemas.jar supplied to me and am trying to access it from within a simple Maven project. I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
When I am trying to access the created documents with something like:
GetOrdersRequestDocument getOrdersRequestDocument = GetOrdersRequestDocument.Factory.newInstance();
It complains about the GetOrdersRequestDocument (can't find it).
How do I get the project to pick up these classes? Do I need to import anything specific?
I have put the schemas.jar file in my src/main/resources directory, which is in my classpath.
Yes, the files in src/main/resources directory are on your classpath. But this doesn't mean that the content of the jar itself is directly available. You could use a URLClassLoader to load the JAR though.
But... this is not how I would do things. If this is an option, I would just install the JAR in your corporate or local repository (using install:install-file) and declare it as a dependency. This would make the content of your JAR available to your code, like any other dependency.
Related
I have a requirement where I want to find the path of a jar that is inside the running jar. Let's say there is a jar called example-1.0.0.jar inside a running jar called main-1.0.0.jar. I want to know the path of that jar that is present inside the running jar.
Edit 1:
For example, the jar is a dependency in the project. For example, let's say I have a log4j dependency in my project and I want to know the path of the log4j jar from a class. How can I get the path?
A jar is just a ZIP file. So once you know how to access the 'running' jar, open it using ZipFile and scan it for the entry you like. Once you have the entry you also know the path inside the ZIP.
If by 'running jar' you mean something on the classpath you could use any classes' getClassLoader().getResource() method but in this case you'd have to know the path to the contained jar upfront.
In the Spring Boot's docs here, about serving static content, it says:
By default Spring Boot will serve static content from a directory
called /static (or /public or /resources or /META-INF/resources) in
the classpath.
I found that all the content in the directory:
src/main/resources
will be copied inside the classpath, so I can put my static content in:
src/main/resources/static
and all will work fine and I'm happy since I can have my static content under the src directory.
But, I have some questions about this:
Why the documentation doesn't say to put static content in src/main/resources/static instead of speaking about the classpath (I think this is a bit confusing)?
Is it good to assume that the content in src/main/resources/ will be always copied in the classpath?
Is there some Spring Boot official documentation explaining what I'm supposed to find in the classpath other than Java classes and packages (up to now I only know I can found all the content from src/main/resources/)?
/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.
I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.
The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.
The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources. So if the documentation would only state the folder src/main/resources/static, it would be incomplete.
Ad 2: As long as you don't mess with the default Maven configuration, then it's safe to assume this.
Ad 3: Maybe start with the official Oracle documentation: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html. Hint: Of course, it's not only the contents of the resources folder, which are in the classpath, but also of course all compiled classes, hence its name.
To add to previous answers, it's the Spring Boot Maven Plugin (spring-boot-maven-plugin in the pom.xml) that that enables you to run the application using Maven. One of its functions is to ensure contents in the app including dependency libraries are available on the runtime classpath, such as within the the executable JAR file.
I am trying to access some WSDLs and XMLs files from WebApp which i built in Maven as a WAR. Now I knew that the resources folder was the default location for these types of files, but when I checked the war package, I found that the files ended up inside the /WEB-INF folder, which is of not much use. Just wanted to know where do I put these files so that I can access them via URL:
https://server:port/Context/File_Path
I tried to make a folder inside webapp but outside WEB-INF, it did seem to do the trick. But just wanted to know if that the right approach.
Maven has a concept of standard directory layout
So, yes, in order to work with something that can be packaged into WAR with maven-war-plugin you should follow the convention and place the files into the relevant folders (of course you can change these defaults if you wish).
When it comes to the static resources, indeed there is a special src/main/webapp folder. So you're right
I have a function that requires the path of a jar file
builder.add(EventAnnotator.createAnnotatorDescription("/org/apache/ctakes/temporal/ae/eventannotator/model.jar"));
This refers to the jar file in my resource folder (as far as I can understand).
I have the same jar file in my maven local repo. and want to use it instead.
Is there a way to pass it as a string like this ?
well it seems we could directly use the path of the jar file, as per the structure of resources (if you put it there). Previously this wasn't working for me as I had a few other errors.
I have this project that it has this structure
Home
graphics
field.txt
example.java
I need to load field.txt in my example.java in jar and I use:
getClass().getClassLoader().getResource("field.txt").toUri();
but this code it give me "Null Pointer exception" .Anyone can help me?
example.class.getResource(“/graphics/field.txt“);
The class should belong to the same jar. For Class.getResource a relative “field.txt“ is possible (same package). With ClassLoader an absolute path for all classpaths is sought: “graphics/field.txt“.
To immediately read (never write) use getResourceAsStream or the URI of the getResource. One can use a Path on the URI. Files.copy(...).
One cannot at least should not) write a resource file (as it can reside in a jar jar:file://...; and jar might even be sealed; and resources might be cached by the jvm). Keep it as read-only template. Never File.
One technique is to create an application named directory in System.getProperty("user.home") and copy the template there.
To read the file it must be in classpath, you can put the file in the folder containing .class files or add it to the classpath with java -cp option.
The issue is not so much your code, but how you build and package your jar file. You will have to clarify how you are currently building your jar (using ant, maven, eclipse, etc ?).
Many articles will also advise you to separate out your resources from your source code (.java), and many IDE will support this separation direclty by allowing you to mark a folder as a resource folder. Even maven will allow you to customize this.
See following articles:
How to package resources in Jar properly
Using maven and netbeans, it is real simple: https://coderwall.com/p/d_cvrq/how-to-package-non-java-code-files-resources-in-a-jar-with-maven, or
use maven to pack javascript files in Jar?