Import/use code from an other Intellij project - java

I have two Intellij projects and in one of them I want to use some classes from the other. I'm not interested in making a library, because I want both projects to update if I change the code in the shared classes.
I have tried with modules, libraries and dependencies, but it won't work.
Thank you.
EDIT:
Followed the answer, this still doesn't. Picture show dependencies.

Perhaps this is the step you're missing.
Suppose Project_B depends on Project_A. To make one module depend on another
open the "Project Structure" dialog
click on the module that will have the the dependency
click the "Dependencies" tab
click the "+" (it's platform dependent as to exactly where it's located)
click "Module Dependency..."
when the dialog opens, click on the project to depend on

I'm not entirely sure if this is what you want but it is possible to share these files using some sort of version control.
Personally I recommend Git, it's reliable and easy enough to use.
You could use commits, push and pull to sync your files.
for more info on git: http://github.com
EDIT: I found a question on stack overflow that might be able to answer yours:
Sharing Java packages between modules in IntelliJ?
This user made a third module and put the shared data in that, and then linked the third one to the first two. Maybe if you set it up like this it will work for you.

Related

Java: Proper organization of external libraries?

So I took a Coursera course that had me work with external libraries. Two .jar files which most of the weekly projects depended upon.
I'm not entirely sure how eclipse compiles and runs the files, and how it links to these external libraries -- what is the proper way of organizing this? Do I put a copy of each .jar file in each project directory?
Is there another, cleaner way that I should be organizing this?
Thanks --
As a beginner programmer it is OK to put it in each project. But consider that this is ongoing work and at some time in future you want to upgrade to a new version of these external libraries. Then you would have to copy it everywhere.
Instead another option is to store them in one place and add it in classpath in each project. Now you have only one copy of it, which is always better.
Now, if and when you do get a new version then the file names might change, so you will still have to change the classpath of each project.
But I advise you to worry about these and other such problems later. For now, focus on programming related problems rather than configuration.
If you want to maintain your libraries professionally in a formal manor then you're better of using a build tool like Gradle of Maven.
I'd suggest you to use Gradle to maintain the project since it has a whole lot of useful build tools available to you to use. Eclipse has a Gradle plugin available which allows you to use Gradle projects with it. See link below.
To give you a idea of how Gradle is used professionally. Android uses it by default to maintain their projects now. So Android java projects uses the Gradle build tool to maintain its library sources, compilation processes and such.
The difference between a Gradle project and a normal java project is that a Gradle project has a list of pre-defined scripts available to you which fetches the libraries, compiles them and prepares them before exporting the final bundle (jar). So really all Gradle does in before hand is fetch the libraries and prepares the specified tools before compilation so you won't need to mess with them your self. It prepares your project directory and remotely maintains your libraries so if they're available from a repository then it'll make sure to prepare them appropriately in before hand and setup your projects directories.
So really the difference you'd physically notice is that instead of using the default Eclipse export button to create your bundle (jar) you'd instead use a button from the side menu which the Gradle plugin adds and also you'd cleanly list the libraries in a structured order in a file that gets added to your project root.
If you want to get a basic understanding of how it works and really want to start to proffesionally or formally structure your project then try to create a very basic android app in Android Studio. see link below
If this isn't what you want at all and don't want to take it to this advanced level yet then adding the library bundles into some kind of lib folder that's located in your project root is properly best practice.
If you wonder why? Well basically different projects might use different versions of the library which may add or remove support to them. So to keep the versions consistent and make sure to have the right version available to you, you have the direct source near the project it self.
Here's some useful link:
http://www.vogella.com/tutorials/EclipseGradle/article.html
http://developer.android.com/sdk/index.html

Stop IntelliJ polluting Project tool window with module names?

On the back of this question, is there a way in IntelliJ to disable displaying the full .iml module name [in bold and in square brackets] next to folder name when it is simply a fully qualified version of it? It is unnecessarily noisy on a big project...
http://i.stack.imgur.com/eE5nz.png
There is a related feature request open in IntelliJ IDEA issue tracker:
IDEA-82965 Clean module names
Feel free to vote/comment.
As a newcomer to both IntelliJ and Maven, I also found the module names to be noisy. Perhaps I'll need them later, but for now I needed to be able to focus on the file directory structure and just needed them gone. Switching from "Project" to "Project Files" did the trick. The Project Files view has an option in its Options Menu to Show Modules that I was able to disable. Maven module information is no longer visible. It might not be the perfect long term solution, as I don't fully understand the difference in the views yet, but it did reduce the as-yet-unnecessary information I was being bombarded with.
(Thanks to https://youtrack.jetbrains.com/issue/IDEA-126014 for pointing this out while working on a similar issue.)

Closing unused modules in intellij idea like in eclipse

As I know for now there is no any feature to do that in intellij idea. I dont know why but they dont support to do that, at least this is the result which I found with all my researching. Maybe some of us manage this problem with different ways. How do you work with multiple modules in intellij? How should I increase the performance while working with multiple projects?
Closing unused modules in intellij idea like in eclipse?
You can make that module directory as excluded from that project.
Right clicked on the directory, then goto Mark Directory As -> then click Excluded it
To Add a module back, click on Project Structure button, then goto Modules section, then you can add them back
In Intellij 2017.2, you can easly load/unload modules or groups of modules :
Right click in the "Project" view
Select "Load/Unload modules"
Select modules to load/unload and click OK button
Warning : This is an "experimental" feature
Ref : jetbrains site
The Offline Module plugin let you disable or enable modules in project without deleting.
Click Disable Module in context menu in Project View, it disables the selected modules. Disabled Modules are shown in a group and can be add back by Load Module.
Projects in Eclipse are Modules in IntelliJ. I do not recommend using them the same way - ie importing unrelated projects as modules into one IntelliJ project.
Importing for example database project and application project into one is ok, or when working on one project means working on another.
But when you import too many projects into one, you will have a problem with searching and "Go To File/Class" actions, and you will use too much heap space.
Generally, when you have a lot of opened projects, there are three problems
too much used heap -> GC pauses.
switching between them is hard
closing and reopening a lot of projects is hard
To solve this, I have made Frame Switcher plugin for easier switching between projects, and reopening closed ones.
Also, when you have 32 bit Windows and have too many projects, then you need to start new IntelliJ instance, and Frame Switcher can integrate them.
There is also Missing Functionality plugin which lets you "Close All Other Projects".
Since the old "Offline Module" doesn't work, I made a plugin to utilize the mechanics of "Mark as Excluded" to quickly show/hide multiple modules.
Link to module here: https://plugins.jetbrains.com/plugin/7803?pr=idea

Using a Library in Source Code in Eclipse?

It's been about 6 years since I had to do any Java programming, and even longer since I had to do any notable amount of Java programming. While I remember the language, I was always weak on all the other things, such as all the tools for building programs and such. In fact, I've forgotten more than I remember - and I was self taught in the first place.
In the past I based my organization of code on what I had seen in some open source projects, so I had directories set up with something like com/mybiz/util and com/mybiz/network and so on. I'd put the source code for the classes in the appropriate directory and make sure it was in the package that matched that path. Then if I had to change the code (like for a bug fix or to add a new routine in an existing class), it was easy for me - change it and recompile the class. As I recall, imports for the classes in the root directory for my project (it was all tied together) to use these classes were no problem with that setup.
Then someone told me about Eclipse, but the biggest thing I remember doing was refactoring in it. Until then, my IDE was a console window and a text editor.
So I still have a lot of classes in that hierarchy - com/mybiz/util (and so on). But now I'm using this code for personal libraries, so it's in com/tango/util and com/tango/network and such. I've having to make changes here and there to code to make it more universal and to remove stuff that was specific to the business for one reason or another.
I want to use these classes as libraries for my projects in Eclipse now. I'd rather not just compile and put them all into a jar, since many of the classes are still being fine tuned and need recompiling. I'd rather just be able to tell Eclipse, "Use this bundle of source code in the "com/tango..." directory tree and then just use something like "import com.tango.util.FileUtils" in my source code.
Even more, I'd like to be able to specify this as a library or some kind of available source code or resource in Eclipse so it's easily added (or added by default) to each project I create.
Can I do this? Or should I be looking into something else or another way to handle it instead? Again, I'd rather just have the source code included, since it's still being changed around and being recompiled.
For the refactoring "magic" you want to use Eclipse needs to know all source files to execute, so you have to have all your source code added into an Eclipse Java project.
However, if you want to have a set of classes that are available for multiple projects, nobody stops you from creating multiple projects, and setting up dependencies between them. The easiest way to achieve this is to add a dependency in the New Java Project wizard (be careful not to press the finish button after setting up the project name but use the Next button where you can add existing Java projects into the build path).
If all your source code is available in either a single, or some interdependent Eclipse Java projects, then Eclipse will take care of compiling all the classes. Usually, Eclipse is intelligent enough to only recompile what needs to be changed, so this process is really swift (at least most of the time).
I hope this answer is helpful enough - if not, feel free to ask for further information.
Edit: Adding information about Java libraries support.
If your "library" project does not change, but you have a jar for it (typically a case of an externally downloaded library), Eclipse allows you to define User Libraries - libraries that can be added to build path of a Java project. To create such a User Library, open Preferences, go to the page Java/*Build Path*/User Libraries, where you can define libraries that consist of one or more jar files.
However, if you are developing your own libraries, and your project does not go into a gigantic size (e.g. several million lines of code), I recommend adding the library project as source into the Eclipse workspace, as in my experience that is easier to maintain in the long run.
First, I would suggest using IntelliJ (in my opinion it's much better than eclipse) but it is very possible to do this and simple as well. So to save time lets pretend all the classes you need in the future library are Network.class, FileUtils.class, and Helper.class. First make a new folder on your desktop called My Libraries. Right click on it and hit Send To, then Compressed Zip Folder.:
Once that's done drag your class files into the folder.
Open up Eclipse and choose a workspace. Once you've done that, you should show up with the default Eclipse screen. Now hit the File tab and hover over New, then go to Java Project.
You will show up with another screen. Enter the name for your project and click Next. Hit the Libraries tab and then click Add External Jars.
Now navigate to your Compressed Zip and click Open.
You now have your library added.
Here is a little ASCII Chart so you can remember:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Folder -> Class Files -> Compressed Zip -> Eclipse -> New Project -> Next -> Libraries -> Add External Jars -> Compressed Zip (Library)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Dependency bundle (jar-files/sources/API docs) in Eclipse

I'm developing various in-house extensions for JIRA, the issue tracker we use. So far I worked with Netbeans and everything worked like a charm. However, now I need to switch to Eclipse and I'm having struggle setting up the environment for this development project.
First a clarification why I'm using the approach I'm describing here: building JIRA (in an IDE) is not easily done and I'm absolutely not interested in wasting my time to figure out how to do it. Besides, I don't need to build it, I just want to develop extensions and be able to use the IDE's auto-completion and help support (API docs). Atlassian (the company that develops JIRA) provides a "development" package, but it's just a sorry excuse rather than a real solution.
What I did with Netbeans was to create a library bundle with all relevant jar-files, the Java source files and the API documentation. This way I could use auto-completion, "jump to" the source and the API docs would pop-up when needed.
It seems Eclipse doesn't offer such a functionality, at least I couldn't figure out how to add the sources and the API docs to a "User Library" (which I'd then add as a dependency to my project just as with Netbeans).
My next approach was to create a separate project that holds all the stuff and mark that project as a dependency of my project. This works, but it leaves me with another issue: now I get 37k errors reported (all within the "dependency project"). As said, correctly setting up building for this dependency is a major struggle and not my original goal, therefore I'd happily ignore these errors. Automatic building is turned off and changing the "Errors/Warnings" settings under "Java Compiler" for the project didn't change a thing, so I'm kind of lost now.
Okay, let me try to phrase this as questions:
Maybe I just didn't find it: Is there a way to create a dependency bundle (call it whatever you want) in Eclipse that -- besides just carrying jar-files -- gives me the ability to use the API docs and "jump to" the declaration in the sources?
If not, what's the common practice to do in such a situation?
If the "dependency project" solution is the way to go, how can I completely disable compiler errors for that project?
Check this for illustration with images
Add the source code for jar
Add the Javadoc for a jar
Or just right-click on the jar file in the Package Explorer view. Select "Properties" then set the according paths in the "Java Source Attachment" and the "Javadoc Location" field.
When you create user libs via window->preferences->java-build path->user libraries you can specify which jar you need, sources (in archive or folder) && javadoc (from internet, or local, or from archive). Then you can use this lib in your project via context menu on project->Build path->add library->user library-> choose your lib.
Here you acn pick up more info Eclipse help

Categories