BlaqICE
BlaqICE

Reputation: 309

Creating Java Packages in IntelliJ

I've been using Eclipse for a while and I'm having trouble understanding what's going on with my first project in IntelliJ. I've read the documentation, and searched other questions, but I still can't seem to grasp it. I think there is something wrong with my project structure. This is what my structure currently looks like;

IntelliJ Project Structure

I'm trying to run the JavaForLoop class, but whenever I do, compilation fails because I have errors in the StringMethods class of the strings package. My question is why would that prevent compilation if the two classes are in separate packages? Neither class uses the other, and they both have the appropriate package declaration statements. With a similar structure in Eclipse, this would work. Should I be using a different project structure?

Upvotes: 0

Views: 69

Answers (3)

puhlen
puhlen

Reputation: 8529

Intellij uses regular javac, which will fail to compile if you have errors anywhere in the code.

Eclipse has it's own compiler, that allows to compile and even run code that has compilation errors, causing a runtime exception if any part of the code that has errors is run. This allows you to run parts of the code that work even if other pieces of code are failing.

The simple solution is to resolve your compilation errors. You can also use the eclipse compiler with Intellij, but I've never done this so I can't comment on how well it works.

Upvotes: 0

streetturtle
streetturtle

Reputation: 5850

By default IDEA adds Build Configuration which is executed before launch and includes following steps (taken from here):

  • Compiling source code in the source path of a module and placing results to the output path.
  • Compiling source code in the test path of a module and placing results to the test output path.
  • Creating copies of the resource files in the output path.
  • Reporting problems in the Messages tool window.

enter image description here

check if it's your case in Edit Configuration screen and if so, remove it.

Upvotes: 1

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

To use a class from a different package you must declare a import statement to the class.

In your JavaForLoop.java add the import before the class statement (and after package declaration where its the case)

//package ...
import strings.StringMethods;
//public class JavaForLoop { and the rest of the code

Upvotes: 0

Related Questions