How to add prefix to package names? - java

I've got 45 java packages I've imported into a src folder in Eclipse. Each of course has a particular name, and contains numerous java files. Problem is that the import statements in the packages' classes all refer to the various packages (including themselves in the package line) with an initial prefix name.
So for example, if the package name is
snowman.image
the java files within start with abc.snowman.image
and refer to another package named snowball.hit as abc.snowball.hit. Needless to say, Eclipse complains that these packages referred to in the code do not exist.
I'm thinking I must've done something wrong. Did I import the packages incorrectly? Can I change some property so that the prefix is understood before the packages' names. I'd rather not go through hundreds of java files and manually change the package and import names.

First you need to make sure that the project configuration is correct. Right click on the project, then go to "properties", and in the tree on the left select "java build path". Switch to the "source" tab and and make sure then you have the "your project/src" as a source folder. Starting from the "src" folder you will have the package names just as Tom commented on your question.
Also eclipse has some nice feature, such as "organize imports" - default shortcut ctrl+shift+o - which will look through your source files and update your imports. This works both in the text editor for the current class, as also from the "navigator/package explorer" view for the entire. selected project.
Please make a backup of your project before trying this, as it will automatically change your files by removing unused and adding the imports it finds in your current structure so you might end up with snowball.hit instead of your desired abc.snowball.hit.

Related

Moving multiple classes to another package and reconfiguring the package in each file

I have a lot of classes within a package and I have to move them to another package. I'm using Eclipse IDE and after moving them, I have this problem of editing each class file's first line (the "package" line)
For example, I have to change the line:
package abc.def.xyz;
to
package abc.def.hij.xyz;
is there a better way than to open each file and manually do it? I'm not a familiar with the IDE and I've looked around for a better way in Google but no solution yet. I have a lot fo things to move.
The error will come in case where you are manually moving classes directly to another package in your workspace.
Instead of doing this go to package explorer of your Eclipse IDE, I guess there will be multiple packages in your projectname->src folder(If it is simple java project type).
Now just select the java files from one package in your package explorer and drag them to target package.
Doing this will show a "Move" popup with auto selected checkbox "update references to classname.java", just press Ok.
This will just change the reference of your classes and you do not have to change the package name manually to all of your classes.
I hope this helped.:)

Is there a way to create package-info.java for existing packages in one move in eclipse?

I have some legacy packages which doesn't have package-info.java. For some reason I need to create package-info.javas for them and I found the task really boring because there is no shortcut in eclipse for me to do that. I have to manually use the New > File procedure to create the file, then manually type in those package names.
Is there a shortcut in eclipse for such task?
It's counter-intuitive, but Eclipse's "New Java Package" dialog with its option to create a package-info.java works for existing packages, too.
Just right-click the existing package, select New > Package, and leave everything except the "Create package-info.java" checkbox unchanged.
This will produce a package-info.java that's pre-populated with the correct package name and an empty javadoc comment block (possibly with #author tags and similar default content depending on your settings). Works even if you don't have an existing file to copy from. Tested with Eclipse Oxygen.
There is no easy way to do this as far as I know, at least "the right way."
What you can do as a shortcut is right-click package-info.java and click "Copy." Then you can use the keyboard shortcut to paste the package-info.java in any package. Eclipse automatically changes the package org.whatever.pkgname; command to match the package you paste the java file into.

Eclipse doesn't see class/package in import

I use eclipse.
my problem description:
In import I see red underlyed strings. But if I navigate - I see source code.
How does it dispay?
How can I fix it?
You must adjust the project's build path, so it contains the missing packages.
Long answer
You may navigate every class that is known to the workspace. But if they are underlined it means they are not available when building the project.
Click on the node "Referenced Libraries" to see what's included. If your imported package JAR is not listed there, adjust the build path.
Right-click on the folder containing "com/epam/dentsply/service" and choose "Build Path/Use as Source Folder". Be sure to select the one directly containing the "com" directory with your java files.

In eclipse, is it possible to change what the default package is without changing the source folder?

I'm working with some people on a java project. Problem is, I'm the only one using eclipse. The source files are located in svn in trunk/src/*.java. However, if I import that as a project directory, the default package is "" instead of what the actual project package name is.
Is there a way to change that without changing the source location and the package name?
Thanks!
If you mean that you want code in package foo.bar without having a matching directory folder of foo/bar under some source root - no, I don't think Eclipse supports that. While the convention of source locations having to match package structure isn't enforced by the language specification, it's mentioned there and so widely respected that I think it would be a bad idea to do anything else.
Eclipse requires a directory structure that matches the package structure. There is no option to have some package prefix that isn't reflected in directories.
IntelliJ can work with this, and it's what most people expect to see most of the time anway.
I think you are checking out the incorrect root folder.
If you are trying to work with a collection of source files located under trunk/src/ you may be don't need to check out this folder, because you will loose your reference to the main package (for example foo.bar) because it will be the base package.
You may need to check out the trunk/ folder, because Eclipse expects to find the source files under the default /src folder. Once you have your main root folder (with a lot of files like .project, .classpath inside), it is likely possible that Eclipse will recognize your folder structure and configuration and your project will compile without problems.

How to take package .java files into a Eclipse Project

I've got a copy of a java package, with example implementation. The package is in a structure called com.java.project (folders, containing myriads of .java files) and there's also a few example files (example1.java, example2.java)
The example files have 'import com.java.project' calls in them, that's all well and good. However, I can't seem to work out how to get Eclipse finding the package contents (currently returns "Type not found" for any calls to the project objects.
I've tried placing the com structure in the same level as my example1.java, but that doesn't work. I have read that I need to compile the package into a .jar to get it to work w/Eclipse, but that doesn't seem to want to behave, none of the files compile.
In Eclipse you can right click on the Project Explorer and select "New -> Java Project"
Then you can import your java files by right clicking your project, selecting import from file system. If the class file shows an error, open the class file, the class name should have red curly braces, click on the error tick mark and Eclipse will give you an option to move the file to the correct package which in turn creates the folder structure for you.
1.Can you do a check on the build path of your Java project? (Right-click the project -> Properties -> Java Build Path -> Source tab), and see if the root folder contains the code is properly specified here.
2.Maybe a Project->Clean will help as well.
Hii,
Just Create a new project with File -> New menu
you need to open Java Perspective and there you will find a package named "src" you need to copy your "com" directory and paste it at the src folder.
I hope this will work for you.

Categories