Prevent JAR from being imported into Eclipse Java project - java

I have a Java project which is heavily used by all sorts of other Java and Android projects. The project contains some JAR libraries which shall be used by all projects, except for the Android one (in fact the Android project is a Android library project to be precise).
I marked the JARs as "export" in the Eclipse build path preferences of the Java project. However, the Android project shouldn't import these libraries (as they are Java libraries which make use of some classes which are not available on Android), but it shall import the rest of the code (which doesn't really use the libraries, but they are stored in there for convenience reasons and to ensure, that all other projects use the same library.
How can I prevent the JARs from being exported to the Android projects?

You can prevent all jars from being exported so that only the common project is a dependency for each project that needs it.
Then you can change the build path of each project to only include its necessary jars through the add jar.. dialog in build properties.
That's the easiest way.
A more extreme way would be to move to maven and then eclipse will only include the jars you specify in the pom - though that's a load of extra work for not much gain.
Alternatively, you could split the android specific code into a android-common separate project and then make your common project depend on it and export it - then your android project could rely on this android-common project instead of the existing common project.

Related

Refactor classes into a separate project

I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.

Share sourcecode between android- and javaprojects

There is a class, that compiles with the android platform and the jdk. Now, the idea is, to use that class in two projects, one is a android one and the other result in a java application.
So, I cretae a project with two modules, one for the android and one for the java solution. All works fine, with copy&paste the class-source between the two modules, but that is not very comfortable to use.
So, the question is, how it can be done without copy&paste. I would prefer a intellij solution, but I am willing to change to eclipse if needed.
Thanks for all answers.
FWIW, here's what I have working using NetBeans 7.1.1 ... it's possible that some of these steps aren't needed, but after a couple of days experimenting this seems to work.
Create a "Shared1" Java Library containing the shared classes & their source code
Add the "Shared1" library to the desktop Java project
Create a "Shared" Android project... in Properties, tick "Is Library" and set up a Custom location "../Shared1.dist"
Add the "Shared" project to the Libraries in the Android application project
Add "Shared1" as a JAR library (Custom location "../Shared1.dist") in the Android application project
The classes in the "Shared1" project are now usable in both the desktop environment and the Android environment.
BEWARE!!!! Using java.awt classes in the "Shared" library classes doesn't generate any compiler warnings, but causes the Android application to crash. Subsequent runs also crash, even if the java.awt reference has been removed and Clean & Build done on everything!!!
Create a MyLibrary project containing the shared class, package it as a jar (MyLibrary.jar), and add this jar as a library of the Java and Android projects.
Since you say the class is "common", that implies it doesn't use any Android-specific classes. In this case, you can just distribute the common code as a JAR library to the java and Android projects.
Unfortunately I do not really understand what does it mean project with 2 modules.
Typical solution for this problem is 3 projects:
android project
other project
shared (utility) project
Projects 1 and 2 depend on project 3.
If you are using maven it is pretty simple to do: you just have to add appropriate dependency tag. For example if your utility project's artifact is "com.mycompany.util" add definition like the following to pom.xml of your dependent projects:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>util</artifactId>
<version>1.0</version>
</dependency>

How to add a JAR in NetBeans

Let's say you create a new project, and want it to make use of some 3rd party library, say, widget.jar. Where do you add this JAR:
File >> Project Properties >> Libraries >> Compile-Time Libraries; or
File >> Project Properties >> Libraries >> Run-Time Libraries; or
Tools >> Libraries (Library Manager) >> Library Classpath; or
Tools >> Java Platforms (Java Platform Manager)
All of these dialogs seem to do the same thing but I'm sure they all have their proper usages. Can't find a good "best practices" article online and the NetBeans Help Contents dialog isn't helping with this either.
Right click 'libraries' in the project list, then click add.
You want to add libraries to your project and in doing so you have two options as you yourself identified:
Compile-time libraries are libraries which is needed to compile your application. They are not included when your application is assembled (e.g., into a war-file). Libraries of this kind must be provided by the container running your project.
This is useful in situation when
you want to vary API and implementation, or when the library is supplied by the container (which is typically the case with javax.servlet which is required to compile but provided by the application server, e.g., Apache Tomcat).
Run-time libraries are libraries which is needed both for compilation and when running your project. This is probably what you want in most cases. If for instance your project is packaged into a war/ear, then these libraries will be included in the package.
As for the other alernatives you have either global libraries using Library Manageror jdk libraries. The latter is simply your regular java libraries, while the former is just a way for your to store a set of libraries under a common name. For all your future projects, instead of manually assigning the libraries you can simply select to import them from your Library Manager.
If your project's source code has import statements that reference classes that are in widget.jar, you should add the jar to your projects Compile-time Libraries. (The jar widget.jar will automatically be added to your project's Run-time Libraries). That corresponds to (1).
If your source code has imports for classes in some other jar and the source code for those classes has import statements that reference classes in widget.jar, you should add widget.jar to the Run-time libraries list. That corresponds to (2).
You can add the jars directly to the Libraries list in the project properties. You can also create a Library that contains the jar file and then include that Library in the Compile-time or Run-time Libraries list.
If you create a NetBeans Library for widget.jar, you can also associate source code for the jar's content and Javadoc for the APIs defined in widget.jar. This additional information about widget.jar will be used by NetBeans as you debug code. It will also be used to provide addition information when you use code completion in the editor.
You should avoid using Tools >> Java Platform to add a jar to a project. That dialog allows you to modify the classpath that is used to compile and run all projects that use the Java Platform that you create. That may be useful at times but hides your project's dependency on widget.jar almost completely.
Project Files Services Tabls
go files tabs
drag drop file to libs files hover.
return project tabs and what are you see :)

How to include third party libraries in Utilty Project?

In Eclipse Java EE perspective, how does one add a thridparty Jar to a Utility Project?
To elaborate: In a "normal" Java (Not Java EE) project, there's Referenced Libraries where you can put jars. In a Dynamic Web Project, there's Web App Libraries. In a Utility Project, there's only EAR Libraries, which don't appear relevant (well, there are Referenced Libraries that show up in the Package Explorer in Java perspective, but not in the Project Explorer in the Java EE perspective). I went ahead and added a /lib directory under my Utility Project root, and put a jar there (I forgot if I did that in the Java Package perspective, or just in the file system). I added it to Java Build Path, and everything compiles, including the Dynamic Web projects that reference the Utility Project. But when I deploy to Tomcat, I get ClassNotFoundException for the classes in the thirdparty jar.
How do I add the thirdparty jar to the Utility Project in a way that will make it get deployed as part of the web application?
I answered something similar before.
Eclipse and How it Handles JARS -- Odd Case
In essence, there's a difference between build-time and run-time JAR dependencies. For inclusion in EAR / WAR files, you have to use the "Deployment Assembly" panels.
I am not sure to understand exactly your question, but you can simply right-click on your project, in Properties you select Java Build path > Libraries, and then add your JAR in the list of third parties libraries. This will add your jar in the classpath project.
Another way is to use the (ugly) solution of creating a lib directory, put all your third parties libraries in it, and add this directory in the classpath of your project (like for the previous solution, as you can add a whole directory in Eclipse project).
The last solution is to give the responsibility of the dependencies management to a real build system, such as Maven, Ant+Ivy, Gradle...

How to get Netbeans 6.5 shareable libraries working

I created a Web application in Netbeans 6.5. Now I want to use the Joda Time library. I want to share this library via subversion, because I don't want my team mates to be dependend on some Netbeans configuration.
Just to get the project working, I first added the library to the Netbeans library (Tools->Library). This worked OK. The JAR is added to the classpath, and is also deployed.
But when I create a shared library (via Project Properties->Libraries->Browse/New Libraries Folder), the JAR is not in the classpath. I get the error message package org.joda.time does not exist on the code import org.joda.time.*.
Any ideas?
What is the scope of this library? Is this library used for just this particular web-application?
If so, can we put the library in the WEB-INF/lib directory and check that into subversion as well?
Libraries in the WEB-INF/lib directory should be automatically added to the classpath of the project.
Here is what I did:
Tools -> Library -> New Library...
called the library joda-time
add added the joda-time-1.6.jar file to it
Project -> Properties -> Libraries
under the compile tab
Add library...
selected joda-time
(Edit, think I see the issue now - but perhaps not).
You need to add the library to the compile libraries AND add it to the distribution libraries. Or am I misunderstanding the question?
when creating a 'new project', there is an option to enable 'dedicated folder for libraries'. That way, the libraries will also be committed to the repository and your peer developer can checkout your project with all the libraries, your project has dependencies upon, thereby eliminating netbeans configuration bound.
In scenario where a project depends on JARs which can be placed in different locations for different users, a named IDE variable can be used.
http://wiki.netbeans.org/NewAndNoteWorthyNB65#section-NewAndNoteWorthyNB65-VariableBasedPathsInJ2SEJ2EEProjects
Another option would be to use the Maven plugin which already works quite well in NetBeans 6.5. A Intranet repository for the Artifact Jar files could be placed on a file server, or managed through a Maven Proxy like Nexus.
This blog entry describes a hack that worked in NetBeans 5. I don't know if it will work in NetBeans 6.5. I also don't know if this will work if you are building files nightly on a server.
http://blogs.oracle.com/gjmurphy/entry/using_netbeans_free-form_projects_as
I remember setting up shared libraries like this 8 years ago in JBuilder. I wish Netbeans had it by now.

Categories