I'm using Eclipse and I have two different projects: A and B.
In project A, I have a class classA where I need to call a method methodB() from a class classB contained in the project B, how can I do that?
I've tried adding the project B to project A build path, but still doesn't work.
Thanks.
You need to add another project in "Project" tab, or add class folder of the project in "Libraries" tab ie you may try to add project B to the Run configuration used by project A. Go to the menu Run -> Run configurations, ther you can add the project B in the tab 'classpath' of your run configuration.
Here's an example that you may find helpful:
Project_1 has the following class:
ClassProjectOne.java which consists of:
public class ClassProjectOne {
private int m_Age;
private final int AGE_INPUT = 15;
public ClassProjectOne() {
setAge(AGE_INPUT);
}
public int getAge() {
return m_Age;
}
private void setAge(int age) {
m_Age = age;
}
}
Project_2 has the following class:
ClassProjectTwo.java which consists of:
public class ClassProjectTwo {
public static void main(String[] args) {
ClassProjectOne t = new ClassProjectOne();
System.out.println(t.getAge());
}
}
In order for this to work, you must right click Project_2 and click on Properties. Then click on Java Build Path -> Add... -> Select Project_1 -> OK. This sets a Java Build Path.
If your class is static there is no need to initialize a new instance of it.
Hope this helps.
I've just done what you're trying to do. I called my first project 'project1'. In this projects i have a package called 'package1' which in turn contains a class called 'Class1' containing a (public) static method called 'staticMethod'. I called my second project 'project2' with a class 'Class2' in 'package2'. I added project1 to the build path of project2 and then inserted the statement import package1.Class1 at the beginning of the class Class2.
Put the Project B on the Build path, then do a Clean project from Project Menu option and then use it.
Click in "A" --> Properties --> Build Path --> Projects ---> Add the Project ---> Ok
Related
IntelliJ does not find a main class in my Java application project. The project was cloned from a git repository so had no run configuration. I go to Edit Configurations, add a new Application template, go to Main class: and it says "No matches found in project".
So, manually searching through the hierarchy I find the .java file that contains the main function but it will not accept it as the main class. I've pasted the file below to prove that it has the correct main function.
public class AdvanceWarsGameHandler implements IGame
{
private Image mImage;
private String mTitle;
public AdvanceWarsGameHandler()
{
mTitle = "Advance Wars Game";
mImage = new Image("/OffBrandCerealOopsAllCarries2-01.png");
}
//Game logic unrelated to graphics goes here
#Override
public void update(Game game, float deltaTime)
{
}
//Update, but for graphics
#Override
public void render(Game game, Renderer renderer)
{
renderer.drawImage(mImage, game.getInput().getMouseX(), game.getInput().getMouseY());
}
public static void main(final String args[])
{
//Creating and starting an instance of AdvanceWarsGameHandler
AdvanceWarsGameHandler advancewars = new AdvanceWarsGameHandler();
Game myGame = new Game(advancewars);
myGame.start();
}
public String getTitle()
{
return mTitle;
}
}
So the question is, why is the IntelliJ project not recognizing the main function in this file, or what is IntelliJ looking for as the "Main class" of an application?
Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.
The solution came in two parts
Part 1: Missing compilation directory.
Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.
To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.
Part 2: Setting up the modules
The original project was created in Eclipse which has packages. In order to get those packages to work in IntelliJ, I had to go to the Modules tab of the Project Structure menu and set my src and res folders as Source and Resource Folders. This allowed IntelliJ to find the main() function in my class and the program ran as expected.
This solved my problem though if any of you IntelliJ users out there can see anything bad about what I did to get it working, please comment.
I have just started learning Java using an online course.
When I create a new project with IntelliJ, I don't have the Main class in the src folder. However, the instructor is saying IntelliJ should create the class "Main" by default.
HOW I SHOULD HAVE IT:
-"Name of the project"
-Idea
-src
-"com.myname"
-Main (the Class)
HOW I HAVE IT:
-"Name of the project"
-Idea
-src (which contains nothing)
Therefore, I have to create manually the class "Main" by clicking on "src" and creating a class named "Main" with this code inside:
Public class Main {
public static void main(String[] args) {
}
}
MY QUESTION: how can I set IntelliJ to automatically create the main class every-time a new create a new project?
Many thanks for your help.
Create the project from template:
Command Line App will add a class with the empty main method.
I have googled and searched the best I could, but I still could not find an solution. Maybe my search queries was not correct or details.
I do know there are a number of API changes from java 8 to 10. The jdk structure for 8 to 10 has also a significant changes.
Problem:
I have the following dependencies:
Project A --> Project B --> Project C
Some class in project A will call classes in Project B and B will call C. In Java 8 there were no issues.
After I upgrade to Java 10, a NoClassDefFoundError exception occurs.
I found two ways to overcome the issue
Project A now also depends on Project C
In the Java Build Path tab --> Order and Export tab, checked the Project C checkbox.
Question
Is there a better way to resolve my problem instead of using the solutions I found? Because my project codes are huge and it will take a lot of time to do so.
I would also like to know the underlying cause of the problem if possible.
Code:
ClassA.java (Project A):
package pkg;
public class ClassA {
public ClassA() {
new ClassB();
}
public static void main(String[] args) {
new ClassA();
}
}
ClassB.java (Project B)
package pkg;
public class ClassB {
public ClassB() {
callClassC();
}
public void callClassC() {
ClassC classC = new ClassC();
String info = classC.getInfo();
System.out.println(info);
}
}
ClassC.java (Project C)
package pkg;
public class ClassC {
public String getInfo() {
return "Class c info";
}
}
I also exported a eclipse workspace for my issue. I created this workspace using an older version of eclipse and java.
I can reproduce this. The code compiles, but you get an error when executing.
This is an eclipse bug.
Please report it at https://bugs.eclipse.org.
A possible workaround: Edit the run configuration, go to the Dependencies tab, use Add variable string with the value ${project_classpath}
I want to create simple java project with JUnit, so for example I'm want to write an algorithm like merge sort or some Java class and create test class so I can make unit test for that class.
I've create the project with:
File -> New -> Project -> java -> next and setup the project name and
location
and I want to make the unit test for the class the I've created, and I've tried the following solotions :
solution 1 from IntelliJ IDEA dosc using the light bulb to create the test class
solution 2 using shortcut [ctrl + shift + t]
But I always endup with import static org.junit.Assert.*; cannot resolve symbol 'junit', I tried different unit test library end up the same way.
How to resolve this problem so I can make unit test class in this simple Java project?
You can use Gradle or Maven (my personal preference these days).
But the easiest way is to add the JUnit JAR to your project, write some tests, and execute them in IntelliJ.
Go to JUnit and download version 4.12 of the JAR. Put it in a folder /test-lib in your IntelliJ project.
Create a folder /src and add a package /model and a Java class Foo to it (I'll write you one).
Mark /src as a source root.
Create a folder /test and add a package /model and a Java class FooTest to it (I'll write that, too).
Mark /test as a test source root.
Right click on /test and tell IntelliJ to "Run All Tests".
IntelliJ will run all the tests and present the results in the Run window.
Here's the model class:
package model;
public class Foo {
private String value;
public Foo(String v) { this.value = v; }
public String toString() { return this.value; }
}
Here's the test class:
package model;
public class FooTest {
#Test
public void testToString() {
String expected = "Test";
Foo foo = new Foo(expected);
Assert.assertEquals(expected, foo.toString());
}
}
I'm not sure this is the best solutions but I manage to build the unit test use gradle and maven. like this :
create Java project :
File -> New -> Project -> Gradle -> choose only java-> fill the
groupId and ArtifactId-> choose use default gradle wrapper -> enter
project name and location ->finish
and from the root of the project
right click -> Add Framework Support -> choose maven.
from there I can create the class that I want and make the unit test using the solutions from the question [ctrl + shift +t] .
I've got a problem with a Main class not finding another class being public, in the same folder and the same package. Both classes are named as their files. Here is the part seeming to contain the problem:
The Interface:
package hanoi;
public interface Stack<E> {
...
}
The Over-Class:
package hanoi;
public class DefaultStack<E> implements Stack<E> {
...
}
The Used class:
package hanoi;
public class HanoiStack extends DefaultStack<HanoiDisk> {
public HanoiStack (int a){
for (int b = a; b > 0; b--){
HanoiDisk disk = new HanoiDisk(b);
this.push(disk);
}
}
...
}
Main Class:
package hanoi;
public class TowersOfHanoi{
HanoiStack stack1 = new HanoiStack(0);
HanoiStack stack2 = new HanoiStack(0);
HanoiStack stack3 = new HanoiStack(0);
...
}
File Directory (of both):
...\eclipse\Hanoi2\src\hanoi
Eclipse error: Main class could either not be found or not be loaded
(there is actually a main method in the main class, but the rest of the code gets very complicated and doesnt seem to be interesting right now)
Java Compiler error: could nor find symbol: class HanoiStack
Another hint: a friend of mine is working on the same project, seeming toi have declared the interesting part same as me but not having any issues.
Update: download link to the full program is here
Looks like either Eclipse playing up, or it cant compile the classes for some reason.
1) Clean the project in Eclipse. (Project -> Clean -> Clean all projects) Then restart Eclipse for good measure.
2) Check the folder where the project is configured to build is writable. To check what this is, view the project build path (right click -> Build Path -> Configure build path) under source tab check the output folder.
If neither of these help, could you provide more info where the main class is. E.g. is it in the TowersOfHanoi class?