How to build a Java Servlet in Eclipse? - java

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.

Related

Netbeans generating .jar file

When I clean and build, Netbeans generates a .jar file for my Java project, and it uses a "lib" folder which has all of my external Jar files that I use. Can I somehow tell Netbeans to package these Jar files into the .jar it makes? So that I can run my project as a stand-alone .jar file?
I am using Netbeans 7.1.1 on Mac OSX
The short answer is no, the long answer is complicated.
Firstly, Java does not support embedded Jars (ie, you cann't simply add the depended Jars into the main Jar).
Secondly, uncompressing all the Jars and merging them into one will overwrite any resources/classes that share the same path/name. This may not be an issue in small projects but is a major issue in large ones (we have this problem with the project I'm working on at work).
One solution is to use something like One-Jar, which basically uses it's own boot class loader to all you to reference Jars inbedded within a single Jar.
I've had issues with this when using our own custom classloader, but otherwise it seems to work well.
It includes Ant support, so you can include it in your projects Ant build script, if you not using Maven, otherwise, you'll need to devise your own build process
There is no options in netbeans to do that.
There are some other options that a quick search would help, but requires manual intervention.

Building the same project in Eclipse in different jars depending on a profile

I keep server and client code in the same project of Eclipse. Libraries for both of them are included. Images for the client are also added to the build path.
Now I want to generate jars for server and client, that they include only required libraries, and server jar does not have images included.
Is there a way to do this without maven, etc?
Right-click on the project and go to Export. Select jar from the selection tree and in the next tab, remove the code/resources that you don't want included in the jar. Better still if you keep them in separate projects (even if there is no client java code, you can create a resource project).
However, what you're describing sounds a lot like a WAR file. Contrary to a jar, a WAR file is a deployable jar meant to be added to a web hosting application like Tomcat. I don't know if that's suitable for your needs, but more often than not, you want to have both server and client code together. If your project is dependent upon another project, that project will automatically create a jar that will be included in the WAR.
You can divide your sources into three source (and output) folders within the same project:
src-shared
src-server
src-client
Then create a build.xml (Ant buildfile) which creates a jar from bin-shared + bin-server and a jar from bin-shared + bin-client.
Note: One danger of keeping it in the same project is that you can accidentally use client classes from server classes, or vice versa, which will fail at runtime. To fix this, make three projects instead of three source folders.

Can I refer to a Java source path using a variable in Eclipse

Background
I'm using an Eclipse 4.2 (Juno) release to build a plugin for a Java application. The source code and classes for my plugin all reside within the project workspace. The application jar and its source code are, for various reasons maintained outside of the eclipse workspace and the application jar is produced by another build mechanism.
This isn't a problem as I have referenced the application jar file in my project using drag and drop and the 'Link to files' option specifying it 'Create link locations relative to: MYDEV', where MYDEV is a Linked Resource Variable I have created for each Eclipse installation. This lets me use the same eclipse project on multiple machines where the path to the application jar varies, but is always the same relative to MYDEV.
However, I cannot find a way to associate the application source code with this jar unless I use an absolute path. I only want access to the application source for debugging purposes.
Question
In Eclipse, how do I attach java source to a referenced jar in a way that allows a project to be used on multiple installations where the referenced source code has been relocated?
If the source is outside the proyect, then there's no way to access it without an absolute path.
The best solution is using a symlink inside the eclipse project directory to the actual source folder; eclipse won't notice the source is outside the project directory and everything will work fine, without having to relocate the source.
This is the only way I was able to do what you are suggesting (do not know if there are better ways).
Package a JAR file that contains the source code of the JAR file. Place it inside your project in a folder (you do not need to add it to your class path)
In the eclipse project right click on the JAR file and go to "properties" and then to "Java Source Attachment" From there select "Workspace" button and pick the jar file with the source code we added from the above step.
So the source attachment path will always be relative to the project. That way if you share the project via SVN, GIT or whatever, the source will always be available and it'll work if the user is running eclipse on Windows or Linux.
I've had a similar problem to you where absolute paths were a problem for people using different OS and not even using mapped drives via Samba helped that much.
Hope this helps.
The way I have solved this problem is to use a use defined library in eclipse. Here the steps that you can use to solver this problem.
Create a simple project in eclipse in the directory that contains the jars and the source code files.
Define a java user library add the jars to the user library and for each jar in the library specify the location of the source. If the source files are in a project that is in the workspace then the path will be relative to the workspace folder.
Export the user library as an .xml file
When another use wants to setup an eclipse workspace with the same setting as yours they will do two things.
Import the project that contains the jars and the sources into their workspace.
Import the user library into their workpsace.

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 explicitly add a piece of code to a java project

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.

Categories