1

A Java newbie here. I have created a small desktop application in IntelliJ IDEA 11.1. It consists of several java files with classes plus the main application file Main.java. I would like to make another application, which uses the same classes but a different Main file. I want to be able to change the classes source code from both projects. Therefore I'd like to have a structure like:

IdeaProjects\lib/myclasses\*.java (or similar with additional src if recommended)
IdeaProjects\project1\src\Main.java
IdeaProjects\project2\src\Main.java

How do I do it properly? I've read a lot about modules, libraries, jars and am just lost at the moment :(

texnic
  • 3,959
  • 4
  • 42
  • 75
  • 1
    Use modules and [set up dependencies between them](http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html). – CrazyCoder Nov 20 '12 at 19:13
  • @CrazyCoder Thanks, I seem to have added the module, at least I can see in the project structure tree. However, how do I reference a class from a module? Should I somehow import it in my application? I am still unable to find an explanation on the web, but all the references in my app to classes in this module show "cannot resolve symbol". – texnic Nov 20 '12 at 21:06
  • Use class name completion in the code, IDEA will add import automatically. Note that you need to have the module containing this class in the dependencies of the module that you are editing. – CrazyCoder Nov 20 '12 at 21:33
  • @CrazyCoder I thought so too and tried it already, it did not work. However, in the list of dependencies the added directory was shown as "Empty library" in red. I did not pay attention before. Now I realised that my classes probably are not viewed as a module. After failing to find out how to declare them as such, I just moved the whole project, in which I created them (myclassestest), into the directory \lib\myclasses\, and then it finally has worked. Thanks for help. If you convert your comment to an answer, I'll add details and accept it. – texnic Nov 21 '12 at 08:34

1 Answers1

1

I don't use IntelliJ so I can not help you specifically but You should create one project containing the shared resources and per program another project containing you Main files. Compile this first project into a jar file so you can use it as a library in the projects containing the main files.

In eclipse You can add other projects to your classPath. Maybe this is also possible in IntellyJ.

gadeynebram
  • 725
  • 2
  • 6
  • 22
  • Can I edit a source code of the compiled jar-file? I'd like to be able to edit the common classes as needed from both applications. – texnic Nov 20 '12 at 20:43
  • I've found this other post http://stackoverflow.com/questions/854264/how-to-add-directory-to-classpath-in-an-application-run-profile-in-intellij-idea . Maybe this is what You've looking for. – gadeynebram Nov 21 '12 at 07:49
  • I've seen this post before. However, in my case a dialog promised at step 5 does not appear. Instead, the directory is added and shown as "Empty library" in red in the list of dependencies. I believe, I should somehow announce that my collection of java files is a module. But I do not understand how. – texnic Nov 21 '12 at 08:21