refactor method name in eclipse from linked project - java

I have 6 java projects in my Eclipse IDE (juno). One of them is a dummy project which is not meant to be compiled or deployed, it simple holds common logic for the other projects. The other projects use this common project. I edited the classpath of the other projects to include the common source folder as a linked source.
This works fine, but I'm having troubles when I refactor a class or method name in the common project. The refactoring is not propagated to the non-common classes in the other projects that makes use of the refactored class or method, resulting in compile errors that a certain class or method is not found.
The only way I can deal with this is either:
Don't change the name of anything
Manually change the name in all projects after refactoring
This kind of defeats the purpose of having the common source if a simple edit is not propagated. Is there any way of dealing with this?

Try eliminating the "common source folder" of the classpath of your other projects, and instead set the common project as a project dependency in eclipse. To do that, right-click on the project in the project explorer window, choose "properties", then "build path", then the project tab. Add the common project there. I know this allows things like finding where methods in the library are called from anywhere in the workspace, and I feel sure refactoring will work that way as well.

Related

how do i put utilities.java into a library as a folder so that I can use its methods with multiple java projects?

I have a utilities file named utilities.java that is located in my ...\NetbeansProjects\Utilities folder and I want to use the methods in it in any java project that I work on.
If I right-click Libraries folder, there is an option to include JAR/folder... so I select that and find and select the Utilities folder and Absolute Path to it as shown, which adds the Utilities folder to the Libraries folder, as shown.
But how do I reference the methods that are in Utilities.java?
import doesn't offer any help and all I get on every attempted reference is Cannot find symbol.
I can't turn Utilities.java into a .jar file (can I??) because the option to Compile is grayed out.
What do I prefix to each such unresolved external reference to successfully link such methods to my code?
Whether I try to just import or import static, I get error, with suggestions to change to some inappropriate import.
What's irritating is that I think I've successfully done this before....
Based on your screenshot, it looks like you are using an older version of NetBeans (perhaps v8.2 or similar). I will assume this - but it would be worth upgrading if possible.
Generally, I would recommend creating two NetBeans projects, and then include the utilities project as a dependency in the application project.
You can use the "Library" features of NetBeans (as shown in your screenshot) but I think it is cleaner to just create projects, which can then be managed by Git/Mercurial/etc. as needed, as separate projects, with no need to rely on NetBeans-specific library management features.
Ant-Based Approach
For an Ant-based approach (the default for NetBeans 8), that would be something like this:
The above was created as follows:
Create the utilities project:
File > New Project > Java > Java Class Library
Project Name: MyUtilities
Create your package and your Utilities.java file. Add the code you need.
Clean and build the project.
Create a project to use the utility JAR:
File > New Project > Java > Java Application
Project Name: MyApplication
Right-click on MyApplication in the project explorer and select the Properties pop-up window.
Libraries > Add Project - find the MyUtilities project and select it, click on "Add Project JAR Files".
Now you will see that project's jar file listed as one of MyApplication's library dependencies.
After cleaning and building the project you will see a file structure as follows:
Note: For NetBeans 12, it's similar, except they now make the choice of an Ant project (vs. Maven, Gradle) explicit, rather than the default.
Maven-Based Approach
It's a similar approach: Two separate projects. In this case they are both "Maven > Java Application" projects - there is no distinction, in contrast to the Ant approach.
The main difference is how you include the utilities JAR in your applications. that depends on how you want to manage your Maven artifacts - and is a bit outside the scope of this question. One way is to follow the guidelines here - but I think there are other approaches, also.

Netbeans: "main" class in java project

I have a problem. I had project in eclipse. I changed my IDE to netbeans.
Now i have problem, because in eclipse i had two source folder:
src/resources/main
src/java
All my jars i have in src/java, so i added src/java to my source in netbeans.
But netbeans cant find class with "main" function in this project when this class is in src/java. Set main class in properties doesnt work.
But when i move it to src/resources/main it works fine.
Is it possible to have main class in src/java? Because i dont want change my structure
There are two folders, and two issues. We will fix them one at a time. Then they will be explained.
make a main directory just under src
move the src/java folder into src/main, the result should be src/main/java
Then to fix your resources folder
move the src/resources/main into src/main renaming it at the same time to src/main/resources.
Now the rationale. This isn't really about Eclipse or Netbeans, it is about Maven. Maven imposes a particular directory naming convention. Maven has put some time and effort into the convention, and it is generally well thought out. More importantly, Maven has been pretty popular, and it's convention is seen as a de-facto standard.
Your Netbeans project isn't properly configured. However, you could solve it one of two ways: you could configure every detail, or you could adapt to the standard expected conventions. Since this isn't just Netbean's conventions, but also Maven's conventions, and most IDEs expect these conventions (or at least accommodate them), it is probably a better idea to follow them than to reconfigure you Netbeans to find sources and resources in nonstandard locations.
You can manually specify the main entry class.
Right click on your Project in the project explorer
Click on properties
Click on Run
Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
Click OK.
Run Project :)

Eclipse - record and apply move and rename refactorings to another workspace

I'm currently working in a big java project with quite a few submodules that are worked on by different teams. Some of these teams are building the "framework", others are building the "application" based on the framework.
When the framework guys move or rename a class, the applications guys get compile errors wherever they are using a refactored framework class. Is there a way in Eclipse (Galileo Release) to record the change and update the references in another workspace?
What I've tried so far is creating a refactoring script during the rename refactoring, but when I try to apply that script to another workspace, it fails with The refactoring 'Rename Type' (org.eclipse.jdt.ui.rename.type) cannot be performed, since its input 'xxx.TestClass" does not exists. Well, it does not exist (anymore) alright, but what I want is for all references for xxx.TestClass in my project to be changed to xxx.MyRenamedTestClass. Is there a way in Eclipse to do this with built-in functionality or an existing plugin or do I have to write one myself?
Thanks for your help!
EDIT: By now I found out that the "Migrate JAR"-Plugin provides the functionality I am looking for, although we build our JARs with Maven, not Eclipse. I'm going through the source code now to find out what parts I can reuse.
Answering my own question to get some closure here.
The easiest way to do it is to use the Migrate JAR File... refactoring which uses a refactoring script in META-INF called REFACTORINGS.XML. You can get a JAR with this included automatically by using Export JAR in Eclipse. We build with Maven and thus just do Refactoring->Create Script... and put it into the appropriate position in the JAR.
The JDT-internal code that Migrate JAR executes creates Stubs for the source classes in a temporary source folder, so it actually executes the refactoring first and then updates the references. The user never gets to see these temporary files.

Definining user libraries in Eclipse as part of a project and not a workspace

I saw that in Eclipse I can define User Libraries - to make setting the classpath easier (and probably for other reasons as well).
The problem is, that these libraries are only available in the workspace, and if I want other people using the same project to use them - I need to export my user library and they need to import it.
Is there any functionality like this on the project level? I basically need to have a 'classpath group' - can it be done?
If not, is there an automatic way to auto import the user library to the workspace when importing the project?
I'm using Eclipse 3.6.
JDT has the 2 concepts, user libraries and classpath variables. In the classpath variable, you can add jars to your project. Other team members have to fill in the variables in their workspace so their classpath is complete. This is useful when external jars might be in different locations on each team members local file system.
The USER_LIBRARY is a container for adding a logical group of local jars all at once. For example, the JRE_LIB container represents a number of local jars. But as you've seen, it points to a local set of jars meant to be used in multiple projects (as the JRE is added to multiple projects).
Aside from export/import (which you're already doing), I don't believe you can check CLASS_LIBRARIES into a project's SCM. If there was, the preference page would have a "Configure Project specific settings" link at the top.
Your best bet is to simply add the jars to the project, so they'll be included in the SCM. If they can be in different locations depending on the rest of your team, then use a classpath variable so it can be set in each workspace. That's the least amount of hassle as far as team members checking out the project and being ready to go.
The best way IMO is to use m2eclipse - Maven plugin for eclipse. In Maven all the dependencies are defined in pom.xml and downloaded automatically as needed. This means that the only thing you share with your team is pom.xml - your project definition.
There is a lot more advantages when using m2eclipse vs standard eclipse approch. More information is at http://www.sonatype.com/books/m2eclipse-book/reference/
The way I have used user libraries is for something like Ant. Define a user library "ant" for all the jars in ANT_HOME/lib. If including this in your Eclipse .classpath and then sharing with other users, they will get a build problem report until they create that "ant" user library themselves. It's useful, but you need to share knowledge on how to create the library. If you're using it for simple cases like above, then instructions for adding the right jars to the library are straightforward.
Another approach I've used is to build classpaths pointing into a folder (or folders) defined as a variable in Eclipse. See File -> New Folder -> Advanced -> Link to folder in the file system -> Variables. This lets you setup (again at workspace level) variable references to one or more folders. You can then build your Eclipse classpath/s with reference to the folder/s.
So say in your development environment, everyone needs to have a directory called "thirdparty" containing all the external jars dependencies (probably in hierarchy within that dir: thirdparty/apache; thirdparty/sun; ...). You define "thirdparty" as a variable pointing to wherever that dir is on your current system, you create a folder in your project/s using the variable. You can then setup (and share) classpath using paths into that folder.
It's similar to User Library and with similar limitations. The limitation is that the other users you share your project with must create variable folder/s as you have. But it's more flexible in that they don't have to add the jars explicitly as they do with a library; rather, your classpath/s in Eclipse point into the folder, as required for each project.
Note that although the folder variable is defined at workspace level, it can be reused in multiple projects, each of which builds their classpaths (.classpath files) with different references into the folder).
This is maybe something easier to show than to describe with words, but I hope it makes sense.

How to set up multiple source folders within a single Eclipse project?

I have several somewhat separate programs, that conceptually can fit into a single project. However, I'm having trouble telling Eclipse to make several folders inside a project folder.
A simple form of the structure would be:
/UberProject
/UberProject/ProgramA/
/UberProject/ProgramA/com/pkg/NiftyReader.java
/UberProject/ProgramB/
/UberProject/ProgramB/com/pkg/NiftyWriter.java
That is, ProgramA and ProgramB are both projects (in fact, they're currently existing Java projects), which conceptually fit into UberProject.
I don't think I'm supposed to make UberProject be a Java project; it's not a classpath, for instance. ProgramA and ProgramB do seem like they should be Java projects (they might use different build dependencies as well), but I see no way in Eclipse 3.3 to create two folders under UberProject that are intended to contain Java code. I thought about adding a .project file to each of the two sub-projects, but I'm not sure that's appropriate, either. Eclipse help isn't being helpful, and I didn't see anything on SO about this specific problem.
Just to be clear: assume as given the necessity of the existence of UberProject. UberProject can be a Java project, or not; it doesn't matter. (Incidentally, it does contain other folders that do not contain Java code.)
There are probably several ways to do this:
1) UberProject is your JavaProject. Right click ProgramA -> Build Path -> Use as source folder. Right click ProgramB -> Build Path -> Use as source folder. Both ProgramA and ProgramB will do incremental builds to the same directory.
2) Two java projects (ProgramA and ProgramB). You can use UberProject as your eclipse workspace which would be easiest or you can use an outside workspace and import ProgramA and ProgramB as external projects.
There are probably other ways as well (maven multi-module project). Your choice probably depends on whether you have cyclic dependencies between projects. It should be relatively easy to try both 1 and 2 and see what works best for you.
You can have multiple source directories in a single project, but your description makes it sound like you want multiple sub-projects in a project, which eclipse doesn't allow (as far as I know).
If you really just want multiple source directories, where ProgramA, ProgramB, etc. contain only java files and nothing else, then you can do this relatively easy. Right-click on your project in the package explorer, and select Build Path -> Configure Build Path.... In the dialog that pops up, make sure Java Build Path is selected in the left pane, click the Source tab, then click the Add Folder... button. Pick ProgramA from the list, and repeat for ProgramB, etc.
If you want each source folder to have its own output folder, just check the Allow output folders for source folders checkbox and edit the output folders as desired.
If that is not, in fact, what you want to do, then another option might be to group your projects into a working set. A working set is not an UberProject, but it does help keep your projects organized in the package explorer.
Do you need UberProject? I have the same layout but have multiple top-level projects created with File|New project. If not, can you make it a General rather than Java project?
So you can do it via having two Java projects in your workspace.
Then the question is how to group the two projects together under "UberProject"
One way is to have an "UberProject" workspace, and switch workspaces between UberProjects.
An alternative is to define "UberProject" as a working set (Window:Working Sets) and add PrmgramA and ProgramB as projects of that working set. Select that working set, and you see only those projects.
You can have one java project, and define multiple source folders for it. That is normally do that for "main" vs "test" hierarchies within the same project.
There are ways, and ways. Pick one that works for you :-)

Categories

Resources