dan gibson
dan gibson

Reputation: 3665

Eclipse always wants code to be in com\folder instead of com.folder

I have com.company.database.mysql and com.company.database.sqlite. I only want to include com.company.database.mysql.

My code was organized in folders (eg com/company/database/mysql) but now I want to have it flattened (eg com.company.database.mysql) so that I can include just the mysql code.

Eclipse doesn't seem to let me do this. It complains that the package is wrong "The declared package "com.company.database.mysql" does not match the expected package "" It only works if I unflatten it (ie com/company/database/mysql).

How can I get eclipse to work with code in folders named com.company.etc instead of subfolders?

Upvotes: 1

Views: 287

Answers (4)

Sri Sankaran
Sri Sankaran

Reputation: 8320

The problem is that the Java compiler interprets periods in package declarations to be separate directories. So, if your file DataAccess.java is like:

package com.company.database.mysql

public class DataAccess {

}

The compiler will insist that this file be in a directory named mysql which is in a directory name database which is ... In other words your original directory structure:

src/main/java
   com
     company
       database
         mysql

That is the reason for the error. However you don't have to go to the lengths that you are just to exclude some of the source. You can manage your project's source path (Project properties > Build path) and exclude one or more directories.

Upvotes: 2

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

It's the convention of the java compiler that your source files are laid out in a particular way (depending on the platform/filesystem). If I understand you correctly, I think you are trying to go against that convention.

From http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html:

You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in C:\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\workspace\com\mysoft\mypack\MyClass.java.

Upvotes: 1

cHao
cHao

Reputation: 86535

That's not an Eclipse thing -- that's part of the rules of Java, and thus is how pretty much every Java compiler works. It expects your files to be in folders named like com/company/database/mysql, and it's pretty rigid about that.

In order to change that, you'd need to either get rid of the package name, get rid of the dots in it, put your classes into a jar (which would still contain that directory structure, but you could just have com.company.database.mysql.jar rather than a folder), or change Java itself.

Upvotes: 1

BillThor
BillThor

Reputation: 7586

You can't. The dots in the path are directory separators.

For compiled code you can store the class files in a jar. The directory path inside the jar will still be the the tree you have created.

If you really want to flatten the structure use a different character than a period. However, I would not recommend that you use such a non standard solution.

Upvotes: 2

Related Questions