Eclipse Package Declaration Work Around? - eclipse

The team in which I'm working doesn't use eclipse and wants the src folder to be setup a certain way. I, on the other hand, am using eclipse. They would like the src folder to have, for example, the following structure: src/main/java/com/* However, they would like package declarations to only have com.* in them. If I go to project->properties and in the source tab remove the src folder as a source folder and then _only add the com.* folder as part of the source_ it will show com.* as a package, but I still have to add the whole path from src down in the .java files. Here's an example of what I have to do in order for eclipse to recognize the packages:
package main.java.com.parser;
And what the team wants is to have main and java just be a folder so the package declaration would just be:
package com.parser;
So although the file structure would still be src/main/java/com/parser, the package name would be as stated in the later example.
Is there a way to do this in eclipse? I've seen some people asking similar questions on here, but they seem to have no concern that the whole path must be part of the package declaration.
I would much prefer not to do this this way, but no one else is using eclipse on this project.
Thanks for any help in advance.

In the Package Explorer, right click the src folder and select Build Path > Remove from Build Path
Then find the src/main/java folder, right click java and select Build Path > Use as Source Folder
Basically this tell eclipse that java is the folder which contains the packages and source files.
P.S. This is a very normal project setup for building with Maven. If you download the m2e (Maven to Eclipse) plugins and choose to create a Maven Project (or import from the pom.xml) then Eclipse will automatically know how to correctly locate the source folder.

Related

Maven suddenly wants src/ instead of src/main/java

I'm using Maven 3.3.3 together with Eclipse Mars and m2e. Yesterday, I created a new Java project and converted it into a Maven one.
Afterwards, the classpath contained only src instead of the standard src/main/java. I was surprised but I simply created the missing folders and ran m2e's "Update Project". This didn't solve the problem; m2e still insisted on using only src.
So I edited the classpath manually to end up with with the usual
src/test/java
src/test/resources
src/main/java
src/main/resources
When I try m2e's "Update Project" now, I get this error:
Cannot nest 'foo/src/test/java' inside 'foo/src'. To enable the nesting, exclude 'test/' from 'foo/src'
sigh I went to the command line next and ran mvn eclipse:eclipse to get this classpath:
src/test/java
src/test/resources
src
src/main/resources
Now, I'm completely stumped. Why is that happening?
Open your POM in the POM editor and click on the "Effective POM" tab. Search for sourceDirectory. You'll probably see something like this:
<sourceDirectory>src</sourceDirectory>
When you first converted the Java project to Maven, m2e tried to keep the classpath the same. Eclipse Java projects have a different layout by default. They use src/ instead of src/main/java/. There is no test folder since Eclipse projects usually put their tests into a different project.
To fix the issue:
Delete the sourceDirectory element from the POM (Note: It might be in a parent POM).
Go to the project
Select all the source folders
Remove them from the build path (context menu -> Build Path -> Remove From Build Path).
Update the project
The error should now be gone and the classpath should be correct.
There are two types of folders that you can create: 1) Package 2) Source Folder
1) A package looks like this
To create a package, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Package
A pop up window that looks like this should appear
“Source folder:” That is where you choose your project. If you don’t see anything there, click on “Browse…” and find your project. If I click “Browse…” it shows this:
“Name:” is the name you will give your package.
Note: Packages should be within Source Folders. Source Folders are the parent folder.
2) Source Folders look like this:
To create a Source Folder, right click on the folder you want (in my example, I right clicked on Java Resources) --> New --> Source Folder
Under “Project name:” write your project’s name. If you need help, click “Browse…”
Under “Folder name:” write the folder name you want.
Error: If you’re trying to create a “src/main/resources” folder and you’re running into this error “Cannot nest ‘ProjectName’ inside ‘ProjectName’. To enable the nesting exclude ‘main/’ from
Solution:

Eclipse src folder empty

So for my class I need to use eclipse to do some graphics stuff in Java. I've tried both Eclipse standard 4.4 and Eclipse for Java developers, but with both when I tried to create a new Java project, the src folder is completely empty. Why is that?
Edit: I should add in more information. In the lab section of my class I was able to follow the instructions and create a lab 1 project src folder, which contains many files such as Lab1.java, Polygon1.java and all that. When I am trying to do it on my own machine, it's not working
Because it's a blank canvas!!
Right click on the src file New->Class
Give the new class a name and a package and you're away, you can start coding.
If you want to include libraries create a new lib folder at the same level as the src folder and copy any jar folders into this folder. Right click on the .jar files and click Bukd Path->Add to Build path, you'll now be able to import and classes contained in this jar file(s)!!!
Because you created a new project. If you haven't added any code yet, why would there be code in the src folder?

Importing maven sourceproject into eclipse

I have imported my maven project in eclipse using Import Maven project. It got import in eclipse project explorer, but all the source folder are opening as files and folders, its not opening as java source folder. Since its opening as files and folder, it doesnot have compilation unit, found very difficult to code using it.
What do I need to do inorder to make the source folder as java source folder so that I can code easily?
Select the project and from the context menu choose Maven -> Update Project Configuration (This menu item gets reworded across various maven releases so look for something similar). You may also need to choose Update Dependencies.
In the shell/command line, execute mvn eclipse:eclipse

Import Libraries in Eclipse?

I just recently downloaded the dom4j library, but for the life of me I have no idea how to access it. I dropped it in the plug-ins folder and rebooted Eclipse, without success. For some reason finding a straight answer for this is more difficult that I thought it would be.
No, don't do it that way.
From your Eclipse workspace, right click your project on the left pane -> Properties -> Java Build Path -> Add Jars -> add your jars here.
Tadaa!! :)
Extract the jar, and put it somewhere in your Java project (usually under a "lib" subdirectory).
Right click the project, open its preferences, go for Java build path, and then the Libraries tab. You can add the library there with "add a jar".
If your jar is not open source, you may want to store it elsewhere and connect to it as an external jar.
For the Android library projects, I do it as in the attached screenshot:
Right click the project, select Properties->Android and in the library section click Add. From here you can select the available libraries.
If you are importing a jar file, then importing them as jar or external jar, as other posters posted would work. I prefer to copy/paste jar file in the libs folder (create one if it doesn't exist) and then import as jar.
If you want to get this library into your library and use it, follow these steps:
You can create a new folder within Eclipse by right-clicking on your project, and selecting New Folder. The library folder is traditionally called lib.
Drag and drop your jar folder into the new lib folder, and when prompted select Copy Files.
Selecting the Project tab at the top of the screen, and click Properties.
Select Java Build Path followed by the Libraries tab.
Click the Add JARs… button and select your JAR file from within the lib folder.
Your JAR file will now appear in both the lib and Referenced Libraries folders. You can explore the JAR's resources by clicking Referenced Libraries.

Help me understand Eclipse's Java Build Path

I have been moving my Java projects from Jdeveloper over to Eclipse whenever I have to go back and make a change (I only work with Java projects sparingly).
Everytime I try to create a project in Eclipse (3.3.2) I spend quite some time trying to figure out the proper way to configure the source directories in the Java build path dialog.
The biggest problem I'm having is getting the source directories to match up with the package specified in the source files. For Example my project looks like this:
MyProject
DevelopmentBuilds
MainSRC
The MainSRC directory is also the "Root" package so my classes would be defined as:
package MainSRC.Sub1;
If I set my included directory to blank, the files compile but with many errors because the Packages are not in the right place.
How do I tell eclipse to start at MainSRC for the compilation rather than the children of MainSRC?
Or, should I the path up with one src folder with MainSRC as subfolder?
I basically don't understand how this works.
you need to set MainSRC as a 'Source Folder'.
Apparently, you project root are set as Source Folder.
Enter in project properties:
. Right click over your Project root, and select Properties
. Choose Java Build Path
. Remove all source folders
. Click 'Add Folder'
. Select 'MainSRC'
. Click Ok and Ok
Now, your MainSRC are a Source Folder. Some error occurs inside source files. For agile process, right click over package 'Sub1', and press F2. Rename your package for a new, then all your source files will be put in the correct new package.
[]'s,
And Past
You would need to set the source directory to the root MyProject directory. You would have to tell Eclipse to exclude the other directories (such as DevelopmentBuilds) as they are not source code.
You might find you're better off conforming to Eclipse's expectations and creating a source folder which contains your main package folder.