Java Package Organization - java

I have been struggling for days but I could find a good way to organize packages.I have created some classes and I want to have them in every project like all the java classes but i can't. for example if i create a class and define the package like this
package myclass.importatnt.test;
this class is available only in this project. If I want to have this class in other projects I have to copy/paste the folder myclass in the other project folder and this is really boring. Is there any way to organize the packages so I can use them in any moment?

It seems you have some utility classes that you want to reuse in multiple projects.
Instead of copying the source code of the classes in every project, you should instead compile the classes, package the class files in a jar file, and add this jar file in the classpath of every project needing them.
That's what you do with every other librery that you use (commons-lang, guava, whatever...), and there's no reason not to do the same with your own utility classes.

If your using eclipse, go to Project->Properties->Java Build Path, and add your project to the list of required projects (under the Projects tab). Then you should be able to import them.

Related

IntelliJ ctrl-click references a .class file not a .java file

I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all

Can't make a folder in Eclipse

All I want is to make a folder at the "root" of my eclipse stuff so I can store projects from different classes together. However, it won't let me do that, the "Finish" button is greyed out until I select a parent folder - I must choose from one of the many projects I want IN this new folder. What do I do?
It kind of sounds like you'd be best served by a single project that contains all of the source files from your classes. The workspace is the root directory and any subdirectories are expected to be individual projects.
I agree with what everyone has been saying, create a project for the entire class as 1 project. Then you can create as many classes as needed with main() and only export that single class (or dependent classes as needed) to turn in. In addition, you could package each individual project assignment in a separate package. As stated above, leave your root (workspace) where it is.
Instead of creating multiple projects you may use Java packages option. Create a new package and put all your additional classes over there. Please note that all Java files should be under src/ directory of the eclipse project.

Replace Eclipse library class with my own

I'm developing an Eclipse plugin in which I want to overwrite the functionality of a method contained in a class that is in the Eclipse library.
What I've tried so far is creating an identical (same package and file names) file in my plugin source, and making the changes I want there. Then, I set the build path order such that my source is above the Plug-in Dependencies. Based on my limited understanding, this should mean that when Java looks for that class, it should use mine over the one in the library.
However, this is not working. The behavior that I want to override is not changing, and I don't see the print statements I put in my code either.
How can I "replace" a class in the Eclipse library with one of my own?
I did it once (not proud of it :-)) in the following way:
Import the plugin you wish to hack by Import->Plugin Development->Plug-ins and Fragments (Import as Projects with source folders).
Set the project to build automatically, edit the file and find its resulting class file.
Open the jar of the plugin (the one containing its class files), inject your class file instead of the original one.
If jar file is signed remove all signature information from MANIFEST.MF (and maybe other files).
I admit it's ugly but it's the best way I've found.
I ended up using the JVM JavaAgent to achieve this, by overriding the class loader and loading in my own class to replace the one in the library.
This was a useful tutorial for me: https://stackoverflow.com/a/11898653/634324

How to create .jar library from class in Intellij IDEA

I am trying to learn java and have implemented a single, and simple, class in Intellij IDEA 14.1.3. I want to be able to use this class in other projects without copy and pasting the class source into each project src directory. Essentially, I want to create a package, or module (I'm not sure which, which is part of the problem) that I can simply import into any projects's src that I am working on--kind of like my own, one-class, library. While trying to figure out how to do this, I came across the two following blog posts--
http://blog.jetbrains.com/idea/2011/10/new-in-11-create-library-from-project-view/
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
--but when I tried to do what they instructed (creating artifacts in Project structure, etc.) I wasn't able to because my class doesn't have a main() method, as its not meant to. So I was wondering if I was going about this right--is there a way to do this, or am I stuck with copying and pasting?
I managed to get it--I had to go into Project Structure and set it to create a jar file containing my compiled .class file upon build. Then I could add the jar file as an external library in my other projects. Thanks guys for the replies.
You only need a main method if you want the package to be able to run as a standalone application.
So, to answer your question: no.
You don't need a main method to create a package, nor to import or use the package/library in a separate application.

Use a Java Package Across Several Projects Eclipse

As I do more and more coding in Java I am creating a library of methods that I use often. Is there a way in Eclipse to set it so that I can import these methods from the source folder of the library project to other projects without copying them to each individual project?
I know there is something that I can do with the Build Path, but when I try to add the source for my library I get a package error in the project it is linked to.
Thanks
Don't import/copy the code from another project--the point is to use the output of the shared project. Just indicate that the project depends on the other project; don't think about the shared project's source.

Categories

Resources