Add working directory to build - java

I am having trouble adding dependancies that are located in my working directory, to my .jar. When I try to run the .jar from command line I get errors saying that files I am attempting to access do not exist. Why does netbeans not include the working directory in the build dependancies, seems like a pretty easy and obvious thing to do....
I am simply trying to read a file, located at res/settings/settings.txt. I have made my working directory the res folder so that in netbeans I access that file successfully through 'settings/settings.txt'. I attempted to make the settings folder a library so it would be added to the build, however it refused to copy it because it was a directory.
How can I add this file to the build so i can run it from command line?

Why should it? The working directory is simply a "test" location for your application. The build process is not a "packaging" process.
Netbeans provides you with a build.xml file which can you can modify to perform custom actions, but consider this, each time you build your application, these custom build actions/targets could be executed. That could add considerable time to your build process. If you're just making small changes, this might not be desirable.
Two solutions then come to mind.
You could create custom build targets within the build.xml file which you would need to trigger by right-clicking the build.xml file (in the files view) and selecting Run Target and choosing the required target from the cascading menu...from experience this is not as convenient as it sounds. You could also run the build.xml file from the command line, specifying the targets you want...
Make another ant script which packaged your application separately. This is similar to the first comment except that you completely divorce the build and package processes.
I work on a very large application, which was taking upwards of 15 mins to build (completely). We wrote a utility which went through all the Netbeans project properties and built it's own dependency map, which then generated an Ant build script. We then included this in our own packaging script, so we could build the application and package it within a single pass when we wanted to create a release. This reduced the build down to something like 3 minutes (don't ask why, it just did), it also meant that we could remove the immediate dependency on Netbeans from our build process.
So. The basic answer is. Netbeans is a builder, not a packager. In order to supply this support, you're going to have to write something yourself. The easiest thing to do would be to simply write some Ant script to perform this aciton. Where you do this is up to you.

Related

Gradle JAR add to classpath

I am trying to use a number of external libraries in a Java project. The project runs fine from Intelij but I want to package it in a jar, (or something else) so I can distribute it to others. When I package it in a jar it works if I just do hello world, but as soon as I start using my libraries I get the error bellow. I have also tried packaging it as an application but when I run the batch file it just opens and then immediatly closes a command window. I read all the other posts and nothing is fixing my problem. Please help
My error
My build.gradle
The problem is most likely that your classpath does not point to the correct relative location of your 3rd party libraries. You can check the manifest file to verify if the paths are correct.
However, if it is a runnable jar file with a main method (which it looks like it is), you should use the Application plugin and package it with the Distribution plugin. Right now you are using the Java Library Distribution plugin, which is for libraries. If you do this, you can remove most of the stuff under your jar task.
When testing it locally, run gradle run and when ready, use gradle distZip to create a zip of it all. It will create a script used to start the application with the correct classpath.
Alternatively, you could also package it in a fat jar using the Shadow plugin or similar.

Using external jar as dependency file while running spring boot jar

I have a Maven-Springboot project setup. After doing a mvn install I can run the jar file in command prompt using java -jar <my-jar-file.jar>
There was a dependency to a jar say as-common-1.0.0.jar that is there in my-jar-file.jar. Now I want to override the version of this dependent jar at run time by giving an external jar. something like:
java -jar my-jar-file.jar using as-common-1.0.1.jar
I went through many SO posts like thisinclude external jar when running java -jar but they didn't help.
is it achievable?
EDIT: The issue we have is many of our applications depend upon one of our internal framework jar which gets updated(version) often.
So every time changing the pom file and re-deploying all the apps doesn't look feasible to us. We want somehow the dependency of this particular jar is given at run time. Any idea regarding best possible way to manage this scenario?
No, this is not sensible. At best, you would load classes twice and this may/may not work.
If you build a complete jar containing everything, you cannot swap content at runtime. You need to rebuild it.

How could I have Eclipse export files into a different location than their source?

I am writing a Bukkit plugin in Eclipse in which I separate different functions into different packages and export each package as its own jar file.
However, I would still like to keep these packages in the same project, rather than separating them into different Eclipse projects. These plugins each have files which must be in the root of the jar file, such as plugin.yml. I have moved each jar's files into their respective packages, but these files are put into plugin.jar\com\Preston159\plugin rather than in the root of the jar file (plugin.jar\), causing the plugin not to work.
Does Eclipse have any function to make these files automatically compress into the root of the jar file even though they are contained within the package in the source, or, is this something that I could solve by using Maven? My current solution to this problem is to move the files manually after exporting the jar, but this is becoming increasingly annoying.
EDIT:
The project builder XML I ended up using to complete this task can be found here
You would need to use a Build Tool. There are several supported by Eclipse. Ant and Maven are now built-in, but there are several build tools that run directly within Eclipse, but Eclipse can also be configured to run an external build tool as well.
Do a quick search on build.xml for examples of ANT build jobs.
Unless you're specifically required to use MAVEN for continuous integration, etc. then what you want to accomplish would be easily done with ANT.

Netbeans: Build second JAR in build process

I'm working on a project in Netbeans 8.0. Currently, the build process compiles all of the classes and assembles them into a JAR, as expected. That part works perfectly well and gives me the expected results.
What I'd like to add to the project is the ability to also produce a second JAR meant to be used as a library, containing classes that are useful for making extensions to the primary project. Since all of the classes designated to be in the library are also in the primary JAR, the problem is simplified to essentially building a second JAR that contains a subset of the first one.
The behavior I am trying to achieve is that after editing one or more source files, building the project will build both the primary (executable) JAR, as well as the secondary (library) JAR. I would prefer to avoid using a separate project.
I'm pretty sure that this involves configuring the Ant build process, but I'm having difficulty finding documentation on how to do so. If there is a good tutorial or guide on how the Ant process works and how to configure it, a link would be great! Otherwise, is there a conventional method for configuring this sort of behavior?
Thanks!
Netbeans creates Ant buildfiles for your project that contains many targets for initializing, compiling, packaging, etc. Adapting this automatically generated buildfile (which may be importing other buildfiles) in order to generate your library Jar may not be straightforward because you would have to follow the build procedure and take into account Ant properties, target dependencies, etc.
The basic idea is that in your buildfile, there should be a target that creates the normal Jar of your project. You can place the following task inside that target to create the library Jar. Assuming the subset of your Java classes that needs to be packaged as a library is under package common in the bin directory, the task would go like:
<jar destfile="${myProject}/mylib.jar" basedir="${myProject}/bin" includes="common/**"/>
Check https://ant.apache.org/manual/Tasks/jar.html for how to use jar.
You can also do this in a separate build.xml outside of Netbeans' scope but then of course you would have to manually launch this build.xml whenever you want to build the library Jar.

Packaging and deploying a Jython program from Eclipse

So I've been pigeon-holed into writing some Jython code. I've been using the latest version of Eclipse IDE with the PyDev plugin for development. Up until now, things have been moderately tolerable. I've gotten all my Python scripts working and I'm successfully including a couple of JAR files and the class directory of another Java project as external dependencies. Everything seems to run fine through the Eclipse IDE.
Now I need to package everything up and deploy it. From what I can gather, the best way to do this would be to package everything up in a JAR file. The Jython documentation suggests starting out with the jython.jar file and adding to it. OK. So I modify my main python module and start adding all my python source to the JAR.
It executes but of course can't find all the external dependencies.
How is one supposed to add the external JAR files so that they are correctly seen by the Jython interpreter? How is one supposed to manage more complex dependencies in a setup like this?
Is there a plugin for Eclipse or maybe something like Ant or Maven that can handle all of these steps for me with the push of a button?
I can't be the first person that has needed to deploy Jython code with complex dependencies can I?
I've made some headway on getting this all working so I thought I would put some notes here in case they help anyone else out. I'd still like to hear from others on their experiences trying to put together something like this.
It turns out that Eclipse as of 3.5 has a project export option for Java -> Runnable JAR File. If you use this option, you can point to a Java main class in the export wizard. You also have the option to have it repackage all the JARs that you are dependent on in your new JAR file. Make sure to check the box to save the export as an ANT build so that you can repeat the process quickly. NOTE that the first time you do this through the interface, it may fail, but it will still have created a JAR file.
Now here's where it gets strange. To track all the dependencies, I am still using a mostly incomplete Maven build in my project. I create the Maven .POM file. And I told Maven what my external JAR dependency was. I then told Maven to do a dependency update for me. It pulled everything into my Maven repository as expected.
Now when I do my ANT build, it appears that it is getting its list of JARs to include in the final build from Maven. I'm not really sure if it is supposed to work that way. I'm also not 100% sure that it is working. I guess I'll find out when I have to add another external JAR to the project.
Anyways, if you follow this question you'll see that you can take the latest builds of Jython and pull the org.python.util.JarRunner.java file out and use it in your own project. This is you Java.main class that you will need to point your ANT build at. From there, convert your main Python/Jython script to be the run script that was talked about in that question.
Next, build up another copy of the Jython JAR file in your Jython directory. This one should have the /Lib directory pulled into the JAR. Save that off and then point your Eclipse IDE Jave Build option for your PyDev project at that JAR as an external dependency. Your JarRunner will now work and execute the run.py file under Jython.
If all that works, you should then be able to rerun the ANT exported build from earlier and you will end up with a single JAR file that you can execute on the command line as:
java -jar {yourjar} args
And distribute to your customers without any additional dependencies.
If that all seems a little bit convoluted, it is. If anyone has a better way of setting this all up using Eclipse, please let me know.
Make your life easier and just use Maven and the mavenjython compile plugin.
See my answer of this question: Using Jython with Maven
You get full automation of the build and deploy process, and the result is a jar that includes jython and all other dependencies.

Categories