How do I place files on the classpath? - java

I'm trying to implement a custom view in Spring-Boot-Admin. The documentation states "The JavaScript-Bundle and CSS-Stylesheet must be placed on the classpath at /META-INF/spring-boot-admin-server-ui/extensions/{name}/".
I think I've got the JS and CSS stuff, but where exactly do I have to put them now in my Spring Boot application? This is probably really simple but I don't understand it.

https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
This is the Oracle documentation on the classpath, and is essential reading for any Java developer.
If you are using Spring Boot, I assume you are also using Maven/Gradle or some equivalent. If you follow their standard project structure, src/main/resources will be on the classpath and you can put your js/css there.
However, before going further I strongly advise you to read the above article as it really is quite fundamental to how the Java language works.

They should go to the 'resources/static' folder.

Anything placed in src/main/resources will be placed in your classpath if you are using a tool like Maven/Gradle.
Depending on how you setup your project, you may need to create a fat jar in order for Spring Boot Admin to pick up the files. You can look at the contents of your jar to see if that is necessary. Some IDEs like IntellliJ will create a fat jar for when you run your project from the IDE, but the jars created for deployment are not fat jars.

Related

Add external jar to web project class-path

I have a web application developed in JAVA and I have a jar file. I would like to add the jar file to the application class-path. I know a web application includes libraries from WEB-INF/lib/* but I want to include a jar under this location C:/myLib.jar. Is that possible, if it does how can I do it?
regards,
micuss
ok, jboss is a bit difficult on classloading, but its definitely possible.
you need to add a new jboss module for your external jar and then declare your web application to depend on that module.
there's a complete guide for this here.
its possible to define a module to reference a jar file completely outside the jboss folder (c:\mylib.jar in your case) but it'll be easier on you if youre willing to move the jar into /modules
see here for complete documentation on how to write module descriptors. you can set the path to lead to your external jar (but its gonna be ugly)

Sharing a Spring MVC project

I need to do a Java web project. I'm going to be using Eclipse.
I thought of using Spring MVC. As far as I can tell - it's gonna require me to add some "extra" stuff to a "clean" Java web project. I don't mind that - the thing is - one of the project requirements is - that I'll be able to send the project to someone else - that doesn't have any extra installation and/or configuration - and he will be able to compile the project.
Is that possible with Spring MVC? Does the Spring MVC framework is just a "JAR" like addition to the project - therefore - the project can be shared without a problem?
Thanks.
Short answer: yes, you are right.
To use Spring in your Java web project, you generally need to add two things:
the appropriate Spring configuration file(s) (XML)
the appropriate Spring JAR files (traditionally placed in /WEB-INF/lib, the same as other JARs). By the way, it is NOT a single JAR file, but several JAR files.
That is all there is to it.
Spring has nothing to do with "sharing the project".
To "share" the project with someone else, you put it on the SCM of your choice and your collaborators will be able to get the code and work on it.
Building the project is a different aspect. To correctly compile the project you have to make sure that all the classes and/or jars are visible in the classpath (and runtime if you want to execute the code). Spring is made of a bunch of jars that, depending on which classes of the framework you use, must be in the classpath (eg. putting them in the /WEB-INF/lib directory). You may version them as well, or just version the configuration (I'm thinking about Maven for example, that will take care of resolving the dependencies).
Another piece of the puzzle is making all of this work in your IDE. This is a matter of taste. I prefer not to version ide-specific files (in the case of Eclipse, .settings and .project files/folders). You can do that, making sure you do not use absolute path anywhere, and technically you will be able to import the project without problems from another machine.
Yes.
If the other person has nothing extra installed and configured, namely no build tool, you need to put every needed Spring and other Jar into a folder, typically called "lib" and tell him to add them into his compile process. If he just uses the JDK he will get an enormous command line. It is much better to use a build tool like Maven or Ant+Ivy for building and dependency resolution. But that would be an "extra installation" per your question.
If he has Eclipse installed like you have and you use Eclipse internal for building:
Put the JARs in a lib folder
Configure the build path
Make a local test build
Export the project as zip (File menu > Export)
" The other person needs to import the project into his workspace
The exported project should not have any absolute paths as long as you didn't set some deliberately in the build path.
This works but is not exactly best practice. Installing a build tool like Maven is absolutely worth the time and should be preferred under any circumstances. It will save you a lot of time and nerves.

Make WAR executable like JAR to provide command line interface out of the application (JBoss)

I have a servlet which have some logic and is used via browser like every servlet. Now I am looking for method to do the same thing what servlet does, but it should work like CLI. CLI sends requests to logic deployed on JBoss (classes in war file) and print responses on terminal (command line). It should trigger the same logic and all things should be placed in this one war file.
So let's say I will write java code which provides this funcionality. It should be compiled to jar and placed in war, then this jar should be launched from war. It doesn't have to be a servlet. Only requirement is taht everything should be in this one war file. CLI should be called via one line, something like java -cp jarfileinwar com.blah.Main.
Logic should work on JBoss JVM where everything is deployed, it should work like servlet, but method of call should be CLI.
Greets
An executable jar file has a different structure from what a war file has, so I don't think that you're going to be successful in trying to create an executable war file.
You can, however, repackage your required classes and the classes from your jar dependencies into one big "uber jar", and then make that executable by means of the propert manifest entries.
Doing this is not straightforward by hand, so I'd suggest using a build tool to accomplish this. Maven has two plugins - the Shade plugin and the newer (I think) Maven Assembly plugin that can do this quite easily.
A benefit of using Maven is that you could have two different targets - one to build the war file, and one to build the executable jar file, from the same project.
I couldn't stop thinking about it. As I've commented the original post, I thought it is possible to make WAR files executable by imitating an executable JAR within a WAR.
On top of a Maven 3 web project arche type (as foundation) I've created a little proof of concept project that actually implements this.
Take a look:
https://github.com/is-already-taken/executable-war-example
It uses a custom classloader (this article about classloaders from javablogging.com helped me dong this), a "bootstrap" class that loads the first portion of business logic using our custom class loader and a custom Manifest. The classloader is used to "intercept" the class loading to search classes at the location where classes usually are located in a WAR file and to extract classes from libraries packaged within WAR files using streamed unzip code.
Maybe there's still such a out-of-box solution or something that comes with a Maven plugin, but I didn't found it. Further, this small proof of concept helped me leanring class cloader stuff aswell. ;)
I'm looking forward to comments on my solution and my approach in general.

Maven: How to "best" use maven also for downloding "project related files" like configuration .xmls?

I am new to maven and ran into the following question/problem:
Given Hibernate as example: When I use maven, I can easily mange dependencies by including Hibernate as a dependency. The jars are manged perfectly. But Hinbernate consits of quite a lot of other (config) files that are required to get it running (like hibernate.cfg.xml).
1.) Does Maven provide any solution to also download these files (so I can use them as a basis for my additions. It would not make sense writing these complexe xmls newly from the scratch). => For Example is there "goal/target" in maven that spits out these required "relataed config" files into a specifc directory?
2.) How do you handle this case? Although I use mave, does this meand that I neverless have to download the common zip/gz Project-Files that used to contain these files? (As i did it in the past)=>So maven only manages/solves a part of the "problems" that I have in this regard.
Update: The files I am talking about are normally files I need to edit quite often (configuration files). So they are mostly not provided as a static config file inside the jars.
UPDATE 2 => Real live example: I just started to write a POM for my project and googled the dependency-names, like "hibernate-core", "hibernate-validation", "rome" (RSS Lib), "tuckey" (Rewrite Filter) and included them in my POM. Now I have all the jars downloaded via maven (great!), but however I do not have any (sample/base) config files. The Web-app can not be run...
Formerly (when I did not use maven) I downloaded the official distribution zip/gz package and they contained everything: jars and sample config files. Ok its great that maven helps me with the jars, but in the end I do have to navigate to every project webpage and also download the zip/gz distribuation package (as I did before) to only get the sample config files to include them in my project and then make some smaller changes... (without hibernate.cfg.xml hibernate does not start and writing it from scratch is an absolute nightmare, the best solution is take their sample file and update some specific stuff...
Thank you very much for any advice.
Markus
Most people handle this by including files like this inside their jars and referencing them via classpath.
If this doesn't appeal to you, and you have a bunch of them, the Maven solution is (a) use the assembly plugin to combine them into a jar or zip or tar; and attach the resulting item as an artifact with a non-empty classifier, and (b) use the maven-dependency-plugin to download the artifact and unpack it under target/something.

Spring 2.5 in EJB container

I would like to use the spring framework within an EJB3 project. In detail I would like to use the JDBC template class which should be instantinated from a given data source. When I put the spring.jar to my Jboss lib directoy everything is working fine. But when I put the JAR inside my EAR only there seems to be external dependencies from JDbcTemplate to other libraries. EARs/EJBs classloader try to instantinate the JdbcTemplate and shows me that he can not load the class because of external dependencies. It does not show me which additional JARs I have to put in.
Question: Does some body know which addtional JARs I have to include or even how I can search for depending JARs with external tool. I remember there is a tool which can do this, but I do not know its name anymore. I think something like jarjar etc.
Could anyone help please? Thank you.
This smells like an EAR config problem not an Spring problem. Are you sure that the jar is in the EJB's classpath? You might want to check the MANIFEST.MF file of the EJB's jar to verify this.

Categories

Resources