How to explicitly add a piece of code to a java project - java

I’m working with Net beans IDE and I want to add a piece of code to my project in a way that the source code won’t be visible to the user who is running the project, I decided to export that piece of code as a jar file in eclipse and then add that jar file to the project. But unfortunately net beans does not let me import that. It says which it is expecting a . Or when I insert. It goes in to that class and I cannot import that jar file into the project by import instruction. Is there a command in net beans which creates and object from jar file and then makes access to the functions and classes of the jar file? Would you please give the format of that instruction? If you know a better way to do this task I would be so happy if you share it with me

It's not that Netbeans doesn't let you import the classes but it cannot find the classes you want to import. You should first add the dependency jar file to your Netbeans classpath.
I assume that you are building an Ant project. For a downstream project, right click on the project in project browser then choose Properties menu from popup menu. Select Libraries node from the catagories box. You can now add jar file to your project using the add jar file button on the right.
However, I recommend you to have a look at building your project using maven. You can configure the dependencies using xml. Having said that, you should use whatever you are the most comfortable with.
P.S.
It's "Netbeans", not "Net beans".
You can build a jar file using Netbeans. You don't have to go all the way to Eclipse just to build a jar file.

Related

Including external jars permanently in NetBeans

I'm building a Java Swing Application. This project requires a jar file called JCalender. I've added this to Libraries folder and everything works fine on my computer in NetBeans. But when i open the same project in my friends NetBeans, it doesn't recognizes this library. I've to manually select the file placed inside the projects dist/lib folder. How to avoid this? please help!
But when i open the same project in my friends NetBeans, it doesn't
recognizes this library. I've to manually select the file placed
inside the projects dist/lib folder. How to avoid this?
You can't avoid this because it's not a problem actually. To compile and run a project you need to have access to the external libraries involved in the development, so if you open your NetBeans project in a different computer than yours you will definitely need to resolve the reference to the external libraries. There's no way for the IDE to do it automatically as far as I know.
Can't i give the relative path to the lib folder for that specific
library somewhere in project properties?
You could just give it a try. IMHO if the real goal is to share a project with other developers then I'd change the strategy. I'd create a Library (Tools -> Libraries) and tell my mates to create the very same library including the JCalendar JAR files in the library's classpath. I'd include this library in the project properties and finally I'd use a versioning tool like Git or SVN to share the project.
By doing this your mates still need a copy of the JAR file wrapped in a NetBeans Library, but the project properties won't point to a fixed/relative path looking for a JAR file but wil include a reference to a given Library. The Library itself will resolve the dependency to the JAR file. If you take a look to the project.properties file you'll see something like this:
javac.classpath=\
${file.reference.jcalendar-1.4.jar}
But if you as I've suggested then you'll see something like this:
javac.classpath=\
${libs.JCalendar.classpath}
Here libs.JCalendar.classpath will resolve the dependency so your mates can have the actual JAR file located in whatever folder they like and the project should compile just fine.
Another option is using Maven to manage the projects dependencies but honestly I'm not a Maven expert so I can't help you in this path.
You need to do a "clean and build" and your jar will be in the dist folder. It will include the external jars
You can read more about it here
I think your problem is due to you are providing absolute path of jar file.
while choosing jar on write side of filechooser there is option of
Relative path and Absolute path there you should click on Absolute path.
i am new to java , but anyway , i have faced the same problem and found a solution for my project
If you are in Netbeans , its would be very easy for you
Let you project name is ABC and all your dependent jar file is under the
the folder MyResourceCollection
now we need to permanently import all jars under this folder ,
So from Netbeans ,
right click on your project name
go to properties
go to library
in the library page , check the "libraries folder" label
you can find a browse button at the right side of the label
click browse and select the MyResourceCollection folder
a new window will come , just press Next-->Next--->Finish
all is done , now check yourself by moving the folder into different location

how do I add open source API code to an eclipse project?

I have to write a java application which I'm putting together using eclipse and it relies on open source code. This application needs to be self-contained, meaning that I'm supposed to create a jar file that has no external dependencies.
I can use the open source code when I reference the jar files in the project's build path, but the idea is to have the actual source code as part of the eclipse project, side-by-side with my code.
The source code can be found here: http://hc.apache.org/, but when I import an existing file system into my project I can't quite get things to work. The packages end up with the wrong names, breaking references, and I can't do anything. Notice that the folder containing the source code has this structure:
httpcomponents-client-4.2.3\
src\
httpmime\
httpclient-osgi
httpclient-contrib
httpclient-cache
httpclient-benchmark
httpclient
fluent-hc
each of those subfolders has src/main/java/org/apache subfolders.
Can someone please explain how to do this? Am I supposed to import everything one java file at a time?
Use a tool like OneJar, FatJar, JarJar, etc. to create a single-jar application.
As Charlie mentioned, the Maven Shade plugin is another choice, particularly if you're already using Maven. If you're not, consider it or another transitive dependency management tool.
Some tool should be used, IMO, and it's more important the more dependencies you have.
Alternatively you could use a jar class loader and include the jar file in your artifact.
I would most definitely not include the source of dependencies in your own project.

After building the project from pom.xml using Maven, how do I use its resources

It's been tedious. This is the API I am trying to use. Its resources were set up in a pom.xml which I built using Maven. On built up, it gave me the project socrata-publisher that has
src/main/java the source folder with packages com.socrata.api com.socrata.data, com.socrata.util where each contains only .java
files
JRE System Library and Maven Dependency hierarchies where each contains a number of jar files
Problem is com.socrata.api and the 2 other contains classes which I want to deploy in a project outside socrata-publisher. I tried using import com.socrata.api but it didn't work. Moreover, since its a Java project and not android it doesn't have the is Library option in preferences which could rather give me the solution. Both socrata-publisher and tutorial (where i want to use the resources and which is the android application) lie in the same directory eclipseApps in My Documents.
Here's a little visual queue. Help will be greatly appreciated.
You will need to first of all get the output of the socrata project and all its dependencies.
In command line, going to the project folder of the socrata project, where the pom.xml file is, run MVN INSTALL. You shall see a jar file called socrata-api.jar in $HOME/.m2/repository. If you are using windows and installed MAVEN by default, $HOME should be your user profile folder. Once you see the jar file, add it to your tutorial build path.
I think what you actually want to do is just set up the "socrata-publisher" as a project dependency for your "tutorial" project. That will allow you to reference the built Socrata libraries from the code in your project.
Right click on the project and select "Properties". From within that dialog select "Java Build Path" on the left, then the "Projects" section, and click the "Add" button to add the "socrata-publisher" project.
I think that'll work better than creating a separate jar file that you then include, and then you can also keep the socrata-publisher code up to date using Git.

How to add source files of library jars?

I am using Eclipse IDE and its derivative like Spring IDE for Java development.
In a web application project, I add external jars like Spring MVC jars, Apache commons jars etc to the Web App library folder, hence they are automatically added to the build path. There are many jars in the Web App library folder.
I want to create folder in the project and add all the source files (zip/jar) of the libraries included in Web App library folder, so that I can navigate through the source of libraries from the Java editor window. Whenever I add a source zip/jar file to this folder, Eclipse should detect it and use it whenever I want to navigate to the source of a library.
Is the above possible in eclipse?
Note: I know how to add source files
for each individual jar by navigating
to the build path window and
specifying the source location. But
this is very crude way, and I need to
do for every library individually.
Also the drawback is that source path
is absolute, which means if I import
the project into another computer then
I need to create the source path or
even worse I might have to add the
source files individually again.
One way to automagically get the sources for the jars would be some kind of dependency management system. Most people would scream Maven (2/3) by now, but others exist and work well. Maven does have nice Eclipse integration, so that should be a plus.
The downside is that setting up a Maven project just for it's dependency management can seem overkill. Another point is that all the jars you depend on should be "Mavenized" as well.
As far as I know Eclipse wont automatically detect/scan source archive files and link them up to libraries in your workspace in the way you described it.
I agree with #Gressie on using Maven and the Eclipse Maven plugins -- as in that case it's just a matter of ticking a few boxes and Maven will do that for you.
If however your project is not Maven-ized, you can still do this in Eclipse but it's more tedious:
for each one of the jars in your project (which appear under the dependecies section) right click on it and select properties
in the dialog that pops up you have (at least) 2 locations you can configure: java source attachment -- simply browse to your jar with the sources -- and also javadoc location (point it to the jar with javadoc if you want the javadoc to appear as a tooltip when you hover the mouse over one of the classes/methods/etc in that library).

How to build a Java Servlet in Eclipse?

I'm new to Java. I have to modify this Web Servlet that is running on my Tomcat. The Webapps folder contains several files and directories, but only one .JAR file. I managed to decompile it using Java Decompiler JD-GUI, but when I create the project in Eclipse from the decompiled source and try to Validate it, Eclipse shows me around 389 errors. Errors like
HttpServlet cannot be resolved to a type
And I don't even know which Eclipse button rebuilds the .JAR file. Can anyone help me?
Your eclipse project will need to include the libraries that the servlet relies on to work.
If you create the Eclipse project as a Dynamic Web Project it will set up some of the basic structure needed, including the relevant libraries. You may need to include other libraries if the original code relies on them, but from your description it seems fairly unlikely.
You can create a JAR file from the Eclipse File Menu. click Export..., and select Java->Jar file.
Many projects will use ANT which allows you to define scripts to perform tasks needed to compile, package, deploy and test a project.
you create the Eclipse project as a Dynamic Web Project it will set up some of the basic structure needed, including the relevant libraries. You may need to include other libraries if the original code relies on them,
create a JAR file from the Eclipse
File Menu
click Export..., and select Java->Jar
file.

Categories