A software system is usually developed with multiple modules. However, its structure is continuously modified during software evolution, resulting in poor maintainability and understandability. Therefore, software evolution must accompany system refactoring. This paper describes an optimization approach for package structure according to complex network theory. First, we analyze the relations between classes and build the class dependency graph. Second, we propose a community detection algorithm to recombine the classes and optimize system cohesion and coupling without changing the external functionality. Third, by comparing the original and optimized package structure, the two dimensions of splitting the package and moving classes between packages identify package refactoring opportunities. In addition, we evaluate the impact of the above approach on package quality in terms of package reusability and instability. We design experiments on 10 open-source Java software projects to verify the effectiveness of our approach.