Use JARs of linked projects instead of .class - java

I am building a desktop application with NetBeans 8.0.2. For my application, I have to manage 3 differents projects : The main project, and two "tool" projects that are linked to the main.
When I run the main project, it will check the JARs present in his classpath in order to retrieve the Manifest files and do some work with.
In order to have my application run correctly, it has to see the two linked projects' JARs but it doesn't, because NetBeans deals with the compiled classes of the project instead of the JAR (for debugging purposes I presume).
I found nothing about it on the Oracle documentation, and the only thing looking a bit like what I search is to create a big-fat-JAR by using another component.
Is there a way to tell NetBeans to "compile the linked projects and use the JARs instead of the .class" files ? Thanks in advance
EDIT : Here is an example when I add the project with "Add Project .." option
/C:/Users/xxxxx/Documents/GuiceProjectsRD/xxxReaderRef/build/classes/
And here is an example when I add the JAR
/C:/Users/xxxxx/Documents/JavaLib/xxxReaderRef.jar
When I add the JAR, I have the ".jar" extension which helps me identify a JAR and then look into it for a Manifest. When I add the Project, there is no path to the JAR but only to the compiled classes, and I can't work with that.

I would not depend on the Manifests in Jars since you then get this kind of issues.
Have a look at the Typesafe Config library. It's a small 100% pure Java library to work with Json/Hokon configuration.
Instead of relying on a Manifest, create a 'reference.conf' in each tool project. In your application, create an 'application.conf' (if needed). Load the config via 'ConfigFactory.load()'. It will automatically search all available reference.conf's, and application.conf, on the classpath, whether in a jar or not, and merge those configs into a single configuration.
I use this approach in project to be able to plugin extension. Have for example in tool A a configuration like
tool.A.class = 'my-tool-A.class'
or used nested structures like
tool {
A {
class = 'my-tool-A.class'
}
}
Do something similar voor tool B.
Then in your application, from the Config, you can get a list of 'tool' configs and detect the available tools like that.

Related

Declare dependency to war file in Gradle

Based on the gradle docs, to define external jars means adding to build.gradle the following snippet (considering you have {project_root}/libs/foo.jar) in place:
dependencies {
runtime files('libs/foo.jar')
}
However, using the same dependency declaration for *.war files doesn't work. Is this even possible? The project I'm trying to depend on builds to a war file.
Since war layout is different from jar file standard layout, it's not possible to declare war a dependency file to a java project. Possible ideas:
Clone the project and define it as a dependency (very stupid idea, I'm ashamed that I suggest sth like that)
Contact the author and ask him/her if you can just copy the class you need to use. If you can copy the class along with the credits.
Contact the author and ask him/her if it does make sense to make the codec open source (I know it is right now) and release it as a standalone jar library (maybe along with other classes used in the project).

How to get classLoader of netbeans specify project

I develop a simple plugin for netbeans platform. I wonder how can i get a classpath of specify java project (for example i have opened two WebJavaProject) and now i dont know how can i get a classsLoader for specify project.
Im trying something like but this dosent work:
FileObject f = this.project.getProjectDirectory();
ClassPath cpCompile = ClassPath.getClassPath(f, ClassPath.COMPILE);
cpCompile.getClassLoader(true);
can any body know how can i get it?
You need to look for a more specific classpath. One project will usually contain classpath for sources and classpath for tests. If you start from a project you probably want to access its Sources or SourceGroups (check ProjectUtils.getSources(org.netbeans.api.project.Project) and JavaProjectConstants.SOURCES_TYPE_JAVA). This will give you source roots and there will be classpath associated with them.

2 wars sharing code

I need to create 2 war applications deployed on tomcat server.
One of the applications have the exact same logic and code as the other application but with added changes to the view and controllers.
Then App1 and App2 will have the same code to access data and I don't want to duplicate code.
My idea is create 2 WARs and these WAR files should use a library or other project (I don't know) that has access to the database.
Which solution is the best for performance?
Option 1
If you are sharing code (and it's a big piece of code, which drives you crazy while uploading the war-files) it may be an option to create a jar containing the code and add the jar file to tomcats library-folder, which is ${CATALINA_BASE}/lib/
Note that this is usually not something you want to do lightly, because that jar file will be available to ALL war-files on the tomcat, creating possible namespace-problems.
Option 2
If sharing the code with all the projects on the application-server is not an option you'll have to add the jar-file to the projects and add it into it's classpath (which happens automatically within eclipse if you add the jar into ${PROJECT_ROOT}/WebContent/WEB-INF/lib).
Preformance-wise this doesn't really make a difference since tomcat will load the class-files, which are not very big. The instances might be, but the type of deployment doesn't really have an impact upon instances.
If you want to use the same classes for both projects just simple create one .jar file which will contain those classes. Then add that .jar into your web projects' classpath and use it in both.
You can extract the common part out, and make it as a jar. And then two wars use this jar as library.
If you used maven for building your wars, it would be easier to build a project hierarchy.
something like:
parent
|_common(jar)
|_war1
|_war2

Creating a Jar File Containing Another Jar File

I've created an Android project that does some very simple network stuff. This project is standalone, and I've successfully exported it as a jar file and imported it into other projects I've worked on. When exporting this project, I am ONLY exporting the class files that are used for the project. (No system files: ie. Manifest, etc)
I want to leave this project as standalone since it is used in other projects and will be used more in the future.
However, we have other projects that need the features of the network jar and some other features. What I would like to do is create a new project, import the network jar file. Create wrappers for all the functions in the network jar file, then add more functionality.
Afterwards I want to package this project up as a jar file, so that I can use it solely for projects that require that extra functionality.
Having this project as a jar is important, because this component might be given to other people to use, and easy/simple integration is required.
This seamed like it should be pretty straight forward to me. But I am getting noClassDef errors and Verify errors.
I even stripped the project down to a single function that returns a static String from the original network jar and passed that through and still got a "could not find method xxxx referenced from method xxxx" log and then a unable to resolve static method warning, then a No ClassDefFoundError. Here's the logcat.
What is the proper way to do this?
Thanks!
Jar files that want to use embedded jars must define a custom class loader, like JarClassLoader.
If you want to use a single jar file you also have options like OneJar, JarJar, and ProGuard.

Problems including jar files outside the jar file containing the manifest

Basically let me first explain what I am aiming to do. I have a dynamic ETL transformer app written in JAVA. Due to the dynamic nature of this app I have to be able to add plugins jars to the app in a location outside of the apps jar file.
Basically would like to have the following directory structure:
AppFolder
|- plugins/
|- configs/
|- mainApp.jar
If possible I would like to be able to use wildcards in my manifest to dynamically add jars located in the plugins folder.
Unfortunately all I have tried so far has failed. I have tried to use both relative paths and absolute paths neither have worked (with or without wildcard).
If I however include the plugins folder in the main app's jar file itself it works fine given that I don't use wildcards.
So my question is, is it actually possible to have dependencies outside of a jar or do they always have to be contained within.
The other question is regarding the usage of wildcards. i have looked at [the java documentation] (http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html) to no prevail unfortunately.
some examples of what I have tried so far:
../plugins/*
../plugins/plugin.jar
/abolute/path/to/plugins/*
/abolute/path/to/plugins/plugin.jar
and unfortunately none of them have done the trick so any help would be very much appreciated...
Yes you can have dependencies outside the jar. But wildcards are not supported for specify dependant jars.
The jars need to be explicitly specified in your manifest, and the location needs to be relative to where the application is run from
A better option for you may be to use the Extension Mechanism
java -Djava.ext.dirs=/abolute/path/to/plugins/ ......
If you have control of the code you could always add a JarClassLoader and load the jars dynamically.
http://java.sun.com/docs/books/tutorial/deployment/jar/jarclassloader.html

Categories