-2

Hi I am trying to restructure the package but with no luck. Suppose there are multiple packages in a project like:

main.java.com.wms.package1
main.java.com.wms.package2
main.java.com.wms.package3

I want to restructure it in the below format:

main.java
  com.wms.package1
  com.wms.package2
  com.wms.package3

Think of it this way there are 50 packages that start with main.java. So I want to make main.java as main package and the 50 packages under the main.java package How do I do this?

Pramod Bhat
  • 49
  • 1
  • 6
  • 2
    You don't. The 2 structures you're showing are exactly the sam just displayed differently. – Nicktar Jan 23 '20 at 12:28
  • Java file can have single package. Do you mean import statement? – Masudul Jan 23 '20 at 12:29
  • 1
    Main.java is not part of packagename i think. – Jens Jan 23 '20 at 12:30
  • There are java classes inside package 1 , it is referring to classes inside package 2 and package 3 by importing, but the classes inside packages are without main.java – Pramod Bhat Jan 23 '20 at 12:33
  • 1
    Like jens said main.java is not part of package name – Pramod Bhat Jan 23 '20 at 12:34
  • Looks like you are looking for `package com.main; import com.wms.package1; import com.wms.package2; import com.wms.package3; public class Main { }` – Sudhir Ojha Jan 23 '20 at 12:37
  • change source folder to `main.java` (or better `main/java`), change all `package` statements to remove the `main.java.` part, change all relevant imports to remove `main.java.` – user85421 Jan 23 '20 at 12:38
  • Think of it this way there are 50 packages all starting with main.java.So i want to remove main.java.outside as main package and 50 packages under main.java package. Someone has any solution?? – Pramod Bhat Jan 23 '20 at 12:40
  • @PramodBhat You sure that 50 packages start with "main.java"? Do the sourcefiles contain a `package main.java....` at the beginning? – Amadán Jan 23 '20 at 12:43
  • Yes there are 50 packages that start with main.java. I would have posted the screenshot but its confidential – Pramod Bhat Jan 23 '20 at 12:45
  • @PramodBhat Well, then first: poor you to be in a position to deal with such code! If possible I'd suggest a refactoring then. Basically by convention package names usually start with a TLD and then your company's domain. If that's not possible, look for the options of you IDE. Probably you can hide common prefixes in the package list or something. – Amadán Jan 23 '20 at 14:25

1 Answers1

1

one: "main.java" ist not part of the package names.

two: Although the path-like appearance of package names may trick you into thinking that, Java packages are not hierarchical. That means foo.bar is not a child of foo. If you want the list of packages to be displayed in a certain style, see if your IDE supports that style!

three: If your source files really contain the line package main.java... a refactoring should be considered. If that is not possible, follow option two!

Amadán
  • 718
  • 5
  • 18