Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import .java Files Into the Correct Package

I have a bunch of *.java source files that contain classes in various packages. I am trying to import all of these files into Eclipse projects, but Eclipse always places them in the default package.

How can I tell Eclipse to place each of the files in the appropriate package subdirectory, so I don't have to create all the various packages manually?

I have tried the import from file system feature as well as copying the files into the project, all of which would place the files in the default package rather than those ecplicitly stated in the package statement on top of each file.

Related SO questions only deal with referencing packages outside of the project's src directory hierarchy, manually creating files in the suitable package directory, missing import statements, or bulk-renaming packages, but it doesn't seem like this problem has already been discussed.

Update: As requested in the comments, here are some example files:

Test1.java

package some.random.pkg;

public class Test1 {
}

Test2.java

package some.other.pkg;

public class Test2 {
}

Test3.java

package yet.another.pkg;

public class Test3 {
}

Place all of them in the same directory, then try to import them into a Java project in Eclipse without manually creating the directories for the various packages.

Update 2: In order to clarify the scale, assume that there are actually approximately 100 files like these (of course, with more contents ;-) ) on a weekly basis, which is why I'm looking for an automated solution.

like image 422
O. R. Mapper Avatar asked Dec 19 '12 13:12

O. R. Mapper


People also ask

How do I package a Java file?

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

Which keyword is used to import package in Java?

The import keyword is used to import a package, class or interface.


2 Answers

I was somewhat bored, so I created a Java app to get all .java files from it's current working folder and put them in the appropriate subfolders.

You can then copy/paste the newly created folders/files into Eclipse.

Source code and runnable .jar: https://github.com/felipeweber/OrganizeJavaSources

To execute it you will copy/paste the runnable .jar in the folder you have all your .java files in, and run it with java -jar OrganizeJavaSources.jar from a prompt or command line. It should work in both Linux and Windows (and probably MacOS too).

In my tests it worked perfectly with sane files. Please note I haven't done any error treatments and so fourth, but feel free to contribute.

Good luck

like image 128
beder Avatar answered Sep 18 '22 13:09

beder


According to this link you can get what you actually want to do.

File -> New Project/Dynamic Web Project

then

File -> New Folder, click "Advanced" and select "Link to folder in the file system".

After that right click on the folder and select BuildPath -> Use as source folder.. That is it..

OR

you can create a new folder, copy top-most folder containing *.java files and paste it. Then right click on it, select BuildPath -> Use as source folder...

I hope it works for you!!

like image 38
DarkHorse Avatar answered Sep 21 '22 13:09

DarkHorse