weld-se unable to find bean in different jar on classpath when running from command line - jar

I've got a simple standalone application that uses weld-se, but I fail to get beans detected from all bean archives on my classpath
Basically, I have a bunch of jar files in the directory where I run:
java -classpath * a.b.c.Generator
Two of these jar files are bean archives, and the beans contained in the jar where the weld.initialize() happens are nicely discovered.
However, those from other jar files are completely ignored. This seems strange to me, though I am not wondering if this actually how it was intended. And if so, what would be a different solution?

In my case, I have been unable to make Weld discover the beans in the other jars on my classpath, when running via the commandline like mentioned.
The beanmanager that gets registered discovers only the beans with the jar file where Weld is initialized. Logging indicates this, noting "Bean manager for "/jar/C:/....", so just for one jar file.
To work around this, I have combined all jar files from which I need the beans discovered (with an empty META-INF/beans.xml), and then ran my application like above.

Related

java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver [duplicate]

How should I add JAR libraries to a WAR project in Eclipse without facing java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError?
The CLASSPATH environment variable does not seem to work. In some cases we add JAR files to the Build Path property of Eclipse project to make the code compile. We sometimes need to put JAR files inside /WEB-INF/lib folder of the Java EE web application to make the code to run on classes inside that JAR.
I do not exactly understand why CLASSPATH does not work and in which cases we should add JARs to Build Path and when exactly those JARs should be placed in /WEB-INF/lib.
The CLASSPATH environment variable is only used by the java.exe command and even then only when the command is invoked without any of the -cp, -classpath, -jar arguments. The CLASSPATH environment variable is ignored by IDEs like Eclipse, Netbeans and IDEA. See also java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable.
The Build Path is only for libraries which are required to get the project's code to compile. Manually placing JAR in /WEB-INF/lib, or setting the Deployment Assembly, or letting an external build system like Maven place the <dependency> as JAR in /WEB-INF/lib of produced WAR during the build, is only for libraries which are required to get the code to deploy and run on the target environment too. Do note that you're not supposed to create subfolders in /WEB-INF/lib. The JARs have to be placed in the root.
Some libraries are already provided by the target JEE server or servletcontainer, such as JSP, Servlet, EL, etc. So you do not need put JARs of those libraries in /WEB-INF/lib. Moreover, it would only cause classloading trouble. It's sufficient to (indirectly) specify them in Build Path only. In Eclipse, you normally do that by setting the Targeted Runtime accordingly. It will automatically end up in Build Path. You do not need to manually add them to Build Path. See also How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and JEE libraries which are not provided by the target servletcontainer (e.g. Tomcat doesn't support many JEE libraries out the box such as JSF, JSTL, CDI, JPA, EJB, etc), need to end up in /WEB-INF/lib. You can just copy and paste the physical JAR files in there. You do not necessarily need to specify it in Build Path. Only perhaps when you already have it as User Library, but you should then use Deployment assembly setting for this instead. See also ClassNotFoundException when using User Libraries in Eclipse build path.
In case you're using Maven, then you need to make absolutely sure that you mark libraries as <scope>provided</scope> if those are already provided by the target runtime, such as JEE, Servlet, EL, etc in case you deploy to WildFly, TomEE, etc. This way they won't end up in /WEB-INF/lib of produced WAR (and potentially cause conflicts with server-bundled libraries), but they will end up in Eclipse's Build Path (and get the project's code to compile). See also How to properly install and configure JSF libraries via Maven?
Those JARs in the build path are referenced for the build (compile) process only. If you export your Web Application they are not included in the final WAR (give it a try).
If you need the JARs at runtime you must place them in WEB-INF/lib or the server classpath. Placing your JARs in the server classpath does only make sense if several WARs share a common code base and have the need to access shared objects (e.g. a Singleton).
If you are using Maven:
Open the project properties, and under Deployment Assembly click Add...
Then select Java Build Path Entries and select Maven Dependencies
Resolved by setting permissions.
Had related issue using PySpark and Oracle jdbc. The error does not state that the file cannot be accessed, just that the class cannot be loaded.
So if anyone still struggles, check the permissions. Some might find it obvious tho'.
I want to give the answer for the folowing link question ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse
Ans: In Myeclipse go to Server-->left click on Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)-->ok

Sbt resource generation in runtime

I am trying to achieve what a resourceGenerator in Runtime would do: create a resource that is available on the classpath during runtime, however that would not be packaged under the main configuration.
In my specific case, I am trying to create an sbt plugin that facilitates dealing with JNI native libraries. The above mentioned resource would be a "fat" jar containing a shared library, thus it is not required for compilation but only during runtime.
My goal in the end is to publish the standard jar (in the Compile configuration) and publish the fat jar as an extra artifact (in the Runtime configuration). However, during local testing, I would like the shared libraries to be available on the classpath when simply calling run from sbt.
I tried implementing a resourceGenerator in Runtime, however with no success. An alternative approach I could imagine would be to modify runtime:exportedProducts or alter runtime:managedClasspath directly, however I first wanted to know if there is already a way to include resources only in the runtime configuration?

How to make boxfuse create an image containing executable jar and configuration file to be specified in main arg?

I'm playing around with boxfuse attempting to "fuse" an image which contains an executable JAR. My executable JAR is given the path to a config file as an argument to it's main method, like so:
java -jar my-executable.jar -conf /some/path/to/my/conf.json
Where the file conf.json is read in by the JAR's process to be configured with e.g. port, database connection properties, etc.
I understand how to pass custom arguments using -jvm.main.args="-conf /some/path/to/my/conf.json", however, I don't know how to get the config file into the image itself. Obviously the path has to point to a valid file that exists within the image.
In dev, test and production, I would want to use the same executable JAR, but a different config file for each environment. I don't currently see a way around having different images for each environment. I see there is some support for packaging specific config with Dropwizard payloads, but no mention of something similar for executable JARs.
Is there a more general way I can package arbitrary files into the image, with predictable paths I can refer to in the jvm.main.args?
P.S. in my case the executable JAR happens to be a Vert.x application, but I think the general case applies.
What you can do is package the configuration for all environments (dev, test & production) within the executable JAR file. So you would have dev.json, test.json and production.json
You can then use a technique like environment detection with for example an environment variable to detect the correct environment at runtime and pick the correct configuration, which can then be loaded from the classpath instead of the file system.
This allows you to build both the jar file and the Boxfuse image only once and run it unchanged on all environments.
P.S.: I've just raised an issue for you to add first class Vert.x support in the near future to make things even easier: https://github.com/cloudcaptainsh/cloudcaptain/issues/28

SBT console missing web container's classpath

Using SBT with xsbt-web-plugin, when I go to the console (sbt console), I can't access anything in src/main/webapp that is normally on the containers classpath.
Context is: I'm trying to take certain code that runs in both the webapp and unit tests, and run it in the console. Everything is fine until I reach code that loads one of the view templates stored in src/main/webapp, which it expects to find on the classpath.
The contents of webapp are generally not included on the classpath in deployment (i.e. under standard J2EE containers such as Tomcat, Jetty, etc.), so you might want to move your resources somewhere more conventional, such as src/main/resources which is included on both the runtime and the container classpath.
Here is an example xsbt-web-plugin project that uses Mustache templates that are stored under src/main/resources and thus included on the classpath.

Want to Make a Jar file

I've made a java application(it extends JPanel) and want to make it into a jar file.I have BlueJ and I used it to do so but after I make the jar file it doesn't open. Can anyone help?
Jar files are PKZIP packaged files which have specific folder structure and convention in it. It contains the compiled version of your java sources (*.java -> *.class) plus meta data. You can "open" it with any archive which can handle zip files especially if you append the ".zip" extension to the file name.
If you mean executing your jar by "opening" it, that's a different thing. You have to have a standard static main method in one of your classes so the JVM can find the execution entrance point. If you have that you can say "java -jar yourjarfile.jar", which will execute your class starting from your main. Note, that I assume that you have JVM setup well (I guess you have) and your environment variables should be configured too. In most cases, your jar refers to other jar files. You can provide access for those for the JVM through classpath.
Do you use Eclipse to write code? If you do, then it is easy to use Eclipse to create an executable jarfile.
Do you use a JFrame to present your JPanel? If you do not, you may have a JFrame, it is the "application window" where the JPanel will be presented.
JFrame tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
/Johan

Resources