How to create automatically packages from existing sources in Eclipse

V

vigi98

Hello,

I have a bunch of java files that are in different packages. To create
my project in Eclipse, I have imported everything by a drag and drop
from Windows Explorer to the default package of my project in Eclipse.

How can I create automatically all the packages and move the sources
in theses packages ?

Btw, how can I compile this project, knowing the fact that there is
probably no main as these are classes for a JSP web site ?

Thanks in advance for all the hints.
 
L

Lew

vigi98 said:
Hello,

I have a bunch of java files that are in different packages. To create
my project in Eclipse, I have imported everything by a drag and drop
from Windows Explorer to the default package of my project in Eclipse.

That is probably the worst way to create a project imaginable. You should not
use the unnamed package for anything. When importing a project into Eclipse,
you want to preserve the directory structure that reflects the package
structure of your app.

Web apps also have an overarching structure such as that recommended for
Tomcat, Java Blueprints, or other environments. Basically, web pages go in
the root of your web/ tree, deployment files in the WEB-INF/ subdirectory
under that. The Java classpath starts at WEB-INF/classes/, but that directory
doesn't exist in source, only in the deployment spaces.

Typical source organization:

projectroot/
|
|-- src/
| |-- com/
| | |-- lewscanon/
| : | |-- package/
| : : |-- app.java
|-- web/
| |-- index.jsp
| |
| |-- images/ ...
| |-- WEB-INF/
| | |
| : |-- web.xml
|
|-- build/ <= image of a deployed app
| |-- index.jsp
| |-- WEB-INF/
| | |-- web.xml
| : |-- classes/ <= .class tree starts here
|
|-- dist/
|-- app.war
How can I create automatically all the packages and move the sources
in theses packages ?

Explore the Eclipse File menu for "Import" options.
Btw, how can I compile this project, knowing the fact that there is
probably no main as these are classes for a JSP web site ?

Compilation is easy - you run 'javac' explicitly from the command line, or
implicitly from the command line via Ant, or implicitly from an IDE project.
In the case of a Web project, you also package the compiled code with the web
resources into a WAR (Web ARchive) file. Deploy the WAR to a server like
Tomcat and away you go.

Avoid code in web apps that explicitly writes to System.out or System.err.
 
V

vigi98

That is probably the worst way to create a project imaginable. You should not
use the unnamed package for anything. When importing a project into Eclipse,
you want to preserve the directory structure that reflects the package
structure of your app.

Web apps also have an overarching structure such as that recommended for
Tomcat, Java Blueprints, or other environments. Basically, web pages go in
the root of your web/ tree, deployment files in the WEB-INF/ subdirectory
under that. The Java classpath starts at WEB-INF/classes/, but that directory
doesn't exist in source, only in the deployment spaces.

Typical source organization:

projectroot/
|
|-- src/
| |-- com/
| | |-- lewscanon/
| : | |-- package/
| : : |-- app.java
|-- web/
| |-- index.jsp
| |
| |-- images/ ...
| |-- WEB-INF/
| | |
| : |-- web.xml
|
|-- build/ <= image of a deployed app
| |-- index.jsp
| |-- WEB-INF/
| | |-- web.xml
| : |-- classes/ <= .class tree starts here
|
|-- dist/
|-- app.war


Explore the Eclipse File menu for "Import" options.


Compilation is easy - you run 'javac' explicitly from the command line, or
implicitly from the command line via Ant, or implicitly from an IDE project.
In the case of a Web project, you also package the compiled code with the web
resources into a WAR (Web ARchive) file. Deploy the WAR to a server like
Tomcat and away you go.

Avoid code in web apps that explicitly writes to System.out or System.err.

Thanks Lew,

What I keep from your message is first that I did it the worst way :)
and second that I have to explore the import options to do it a better
way.

Have a good day.
 
L

Lew

vigi98 said:
What I keep from your message is first that I did it the worst way :)

What, me, exaggerate?

Only the part about dropping everything into the unnamed package was "the
worst". The unnamed package, as is well documented [JLS 7.4.2], is only for
quickie toy or example classes, and not for real life.
 
P

Patricia Shanahan

vigi98 said:
Hello,

I have a bunch of java files that are in different packages. To create
my project in Eclipse, I have imported everything by a drag and drop
from Windows Explorer to the default package of my project in Eclipse.

How can I create automatically all the packages and move the sources
in theses packages ?

Does the source code already contain package and import statements? If
so, the best approach would be to import again, preserving the package
structure.

If there are no existing package and import statements, and you now have
flat, default package, project, I would treat the problem as a
refactoring issue.

The Eclipse package explorer right click menu offers Refactor -> Move.
First construct empty packages, then use it to move groups of files from
the default package to the package they should be in. Refactor -> Move
creates the package statements and any required imports in other files.

Patricia
 
V

vigi98

Does the source code already contain package and import statements? If
so, the best approach would be to import again, preserving the package
structure.

If there are no existing package and import statements, and you now have
flat, default package, project, I would treat the problem as a
refactoring issue.

The Eclipse package explorer right click menu offers Refactor -> Move.
First construct empty packages, then use it to move groups of files from
the default package to the package they should be in. Refactor -> Move
creates the package statements and any required imports in other files.

Patricia

Yes the code includes packages and imports. I haev used the import
feature as advised by lew and everything's fine, now. Thanks for your
reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top