Adding xml resource files to classpath in Bazel - java

I'm currently using Bazel on my project, and it needs to load some .xml files which are located in src/resources (you know, the standard Maven file structure). The thing is, when I build the project, this files are not imported into the classpath.
I know that java_library and java_binary have the resources option, but that seems to be for java classes. I tried adding it anyways like this: resources = glob(["src/resources/**"]) but it did not work.
So, how can I add those .xml files to the classpath, so they are added into the compiled .jar?

Solved by using filegroup as per this question.

Related

How to modify a file contained in a JAR that is a dependency in IntelliJ

I am working with Scala in Intellij-Idea, and have run into a problem. I have included the Java tag in case someone with Java experience can also answer my question.
In my .ivy2/cache folder for a certain dependency I am working with, there are two jar files. One contains .class files and one contains .scala source files. I want to be able to modify one of the jars, drop it in my lib folder, then run my program with the jar that includes my changes.
I have an issue though. I can modify the file in the source folder with jar uvf, but when I drop it in lib, and run it, the changes don't show up. I have tried commenting out the dependency in my build.sbt file, but it appears to still be using my old dependency even after I run sbt update.
My question can be divided into several parts:
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
I use Intellij only to edit my files, and use sbt to run the code.
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
That's not possible because the jar file is expected to contain class files, scala files will be handled as resources instead of source files.
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
I would run sbt clean instead, I haven't tried but sbt "show unmanagedBase" is supposed to list you the library if it was actually picked.
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
The ideal way would be to find the complete repository for that source, update the source and run sbt publishLocal, if that's not possible, another way would be to reconstruct the build.sbt and keep adding libraries until the library compiles properly, hopefully, it will have no dependencies, or a few only.

Compiling JAVA project with external libraries, is this possible?

I'm currently trying to compile my project which would include two external libraries.
json-simple.jar and mysql-connector-java-5.1.42-bin.jar
At the moment I compiling my program and the problem is that i need to include these libraries in the classpath of created compiled project, but i do need only that these libraries would inside .jar project compiled file and i can simply run .jar file and make it work withuot including classpaths or something like that.
Is that possible, don't have idea how.
Yes, it is possible.
I'll help you a bit:
How to make an executable jar file?
Basically unpack the files from the libraries into the folder with your compiled classes (you know how to compile them, right?) then prepare a MANIFEST.MF, put it the folder with all these classes, then create a jar as explained in the answers under the link (jar cfm jarexample.jar jexample.mf *.class). If you have any problems then read the documentation of the commands like jar and javac.
Have fun!

How to create Jar file with external folders and Jars

I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html

class not found add jar via eclipse

When I was working with java spring hibernate struts2 eclipse project. I used buildpath-> addjar option to add jars from a folder inside the workspace. Even if the class is available it shows class not found exception.
If i put those jar files in web-inf/lib folder everything works.. Should i keep those files in the lib itself? Can i just include those jars in a seperate folder and add jar using eclipse configure build path??
what is the problem ? please help me... thanks in advance...
If you're using a standard Eclipse Dynamic web project, best way to go is to put all your Jars in the WEB-INF\lib folder. This will automatically add them to your build path and also include them in your WAR when you export it.
If you still want to use the Build Path > Add Jar option on the project, and want your Jars to also be included in your WAR, you have to go to the Project Properties > Deployment Assembly configuration and include them there. Of course this is double the work for every Jar.

How to handle two jar files in java?

I am using Netbeans IDE for a java project. In this project i need a jar file "htmlunit-2.6.jar".
I have included this jar file in the project libraries folder. I have instantiated one of its class "WebClient" but this class needs other classes of "commons-httpclient-3.1.jar" file.
Now I have also included "commons-httpclient-3.1.jar" file in the project libraries folder. But when I compiled my source file, it throws
ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
Kindly tell me how to handle this situation when one class in one jar file needs other classes in other jar file.
Simply put the required jar files on the classpath at compile-time and it should work. If you were doing it from the command-line then it would look like this:
javac -cp jar1:jar2 my.Application
If you are using NetBeans then you need to tell NetBeans that both of the JARs are on your classpath. It will be definable in a Project > Properties wizard as described here and also here from the tutorial
The ClassNotFoundException tells you that your libraries have some dependencies that you don't have included in your classpath at runtime. Your source is OK, because if you have used something not available, NB will tell you this at compile time (or before when editing).
So, welcome in the "dependency hell" of Java. For small projects you will be able to check all dependencies by hand with readme files, docs, etc and put them in the project config as oxbow_lakes said. For bigger things look at maven. It will do (most) everything for you !
(Maven is available in NB6)

Categories