-1

I have made a Java interface that I will be using for a lot of different task over the next few months. I was wondering if there is a way to 'integrate' it into IntelliJ, so that I don't have to copy-paste it to every new module?

Perhaps similar to this: import java.beans.PropertyChangeListener;

This is what my interface contains, and I would like to have it as an importable class, so that whenever I need to use it, I can just add the implements PropertyChangeSubject. This is for my own work and does not need to be shared with anyone.

import java.beans.PropertyChangeListener;

public interface PropertyChangeSubject {
  void addPropertyChangeListener(String var1, PropertyChangeListener var2);

  void addPropertyChangeListener(PropertyChangeListener var1);

  void removePropertyChangeListener(String var1, PropertyChangeListener var2);

  void removePropertyChangeListener(PropertyChangeListener var1);
}

I have tried to add it as a jar file to the global library, and while it show up, I could not make any class interact with it. The jar file was made with command prompt, so if there's a special way it needs to be done, then I probably did it wrong.

Firrez
  • 3
  • 2
  • 1
    It's not very clear what you're asking for here. Do you mean that any time you create a new class, you want it to already have the import there? What is the relevance of it being an interface? Do you mean a Java `interface`, or something else like some sort of UI? Or do you just mean that you have a Java `interface` that you want to be available in lots of different places? – DPWork Feb 11 '21 at 09:46
  • I mean a java interface, not UI. I don't want it to always have the import when I make a new class. But I want to be able to import it like that, when I make another class that will implement that interface. – Firrez Feb 11 '21 at 13:09
  • 1
    In which case the answer from @k314159 is probably the simplest option. I assume this is for a small project like a class or some personal thing, rather than something that you expect to grow into a real production application? If it's the latter, you should read up on build tools like Maven and Gradle, which are the 'proper' way to achieve your goal. – DPWork Feb 11 '21 at 13:31
  • Does this answer your question? [Correct way to add external jars (lib/\*.jar) to an IntelliJ IDEA project](https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project) – DPWork Feb 11 '21 at 13:35
  • I do not have time to test it today but it looks like it, so I will try that tomorrow. – Firrez Feb 11 '21 at 17:05

2 Answers2

1

IntelliJ has a number of features you might be able to use. The one I have had success with in the past is Live Templates.

This works by typing a mnemonic and then pressing Tab, e.g: fori will give you a for loop.

They are easy to create, just copy one of the existing ones and edit until you have what you wish.

Gavin
  • 1,725
  • 21
  • 34
  • 1
    Saving the file as template under 'Tools' tab, makes it accessible when you right click a package and select 'New', even in new projects, so this did what i wanted. – Firrez Mar 07 '21 at 13:42
1

You could package your Java interface into a .jar file, and store it somewhere on your drive. Then, open IntelliJ IDEA, go into File -> Project Settings -> Global Libraries, click on +, and add your jar. Then you will be able to import your interface from anywhere. However, this means that the application won't be able to be edited by colleagues who might not have the same jar in the same place.

k314159
  • 5,051
  • 10
  • 32
  • I have tried this now, and it does show up under external libraries, but I cannot import it. If i double click it it opens fine, and looks like it should, but i cannot interact with it in any way. – Firrez Feb 15 '21 at 14:46
  • @Firrez sorry, I think I misunderstood how to use Global Libraries. It seems you still have to add it as module dependencies as explained in https://www.jetbrains.com/help/idea/library.html - but this would mean having to add them in every place you use it, which is what you wanted to avoid. – k314159 Feb 15 '21 at 15:28