How to add a class on classpath in Intellij? - java

I have the following project structure in Intellij:
I am actually working with a maven project and all I wanna do is just run the project. Unfortunatelly I dont understand why but when I run the class called GenerateGraph it shows me the following error:
Error: Could not find or load main class graph.GenerateGraph
Caused by: java.lang.ClassNotFoundException: graph.GenerateGraph
I dont understand why this happens- it is the first time I encounter something like that. When I go on project structure I see the following:
Run configuration:
seems like it doesnt even see that the graph package has a class inside. Could someone maybe help me understand what has happened? Is it because I renamed the package after importing the project? Thank you very much in advance

Your project is not setup correctly.
You have reverseGraph marked as the sources root. In this case, src is the source folder.
The typical Maven project structure is src/main/java for non-test source files and src/test/java for tests.
What I'd recommend is modifying your folder structure:
Remove reverseGraph
Move src to the root of the project.
Add a src/main/java folder, and move your graph folder into that.
Reimport the Maven project in IntelliJ, and it should automatically flag the java folder in src/main as the sources root.

Most likely the dependencies are not correctly set up - because you do not have this project opened as Maven project in IDE. Please import this project as Maven project, see steps from Open an existing Maven project IntelliJ IDEA documentation section.

Related

How to identify project type from project folder

Recently i have received a project as a zip. I have unzipped and now i want to import the same in eclipse. But am unable to identify what type of project(maven, java, gradle, etc) it is. I have tried importing as existing project into work-space. But its not identifying as a project when searching in eclipse.
For maven projects, it will have a pom.xml, this one don't have one.
Similarly is there any other ways to identify what type of project it is from the project folder?
Thanks in advance!
I have received partial answer from the commends of #Sangeeta and #carchingUp. I want to add all details to a single answer so that it will be helpful to others.
Inputs from Sangeeta: if pom.xml is present you can infer its a maven project. If build.sbt, then its an sbt project. If build.gradle, You can know its a gradle project and build.xml for ant projects
In my case i was having build.xml from which we can identify as ant project.
When you import the project as ant project to eclipse and if you have the below scenario,
"package name is com.abc.xyz, present in src/main/java and when importing as ant project, main.java.com.abc.xyz becomes the project and am getting error for all the java files for package name mismatch", follow the below steps.
right click the src folder >build path >remove from build path
right click the java folder > build path > use as source folder.

Automarking directories in Intellij IDEA

I have a little project that has the next structure:
I'm using Maven to build it. For some reason every time I make changes to pom.xml IntellijIDEA marks java folder as source directory and unmarks the src folder. Why it does so and how to fix or avoid it?

"Could not find or load main class" after removing Maven nature and files (pom.xml, target/) from an Eclipse Project

I accidentally converted my project to Maven by going to Configure > Convert to Maven Project. Now I want to undo this. I read that I need to right click Maven > Disable Maven Nature and that worked fine. However I want to totally remove Maven, so I deleted the pom.xml and the target folder. When I try to run my code now, I get the error:
Error: Could not find or load main class
So what am I missing? How do I revert from a Maven project to a non-Maven project?
When you convert a Java project to a Maven project in Eclipse, the Maven Integration for Eclipse (m2eclipse) configures the Java incremental compiler to put the compiled class files in the same location as Maven would put them, i.e. target/classes.
So when you remove the Maven nature and delete the target folder, you now also have deleted the compiled class files and your project can no longer run. AFAIK, the incremental compiler doesn't detect when you remove its output files, so you need to trigger a rebuild by cleaning the project (Project > Clean...)
This will fix the problem that you can not launch your project, but may re-create a target folder. If you also want this to be "fixed", you can switch back to some other folder name for the binaries, e.g. bin, in the project's Java Build Path configuration on the Source tab.
Is it basically a Maven project, i.e., do you have and maintain it through a pom.xml? Then my suggestion is to delete the project in Eclipse but keep the files on the disk (i.e., it removes it from the workspace). Then, run a simple mvn eclipse:clean eclipse:eclipse which creates a simple Java project without the Maven nature based on the POM (so the libraries are linked and the source/output directories are set up correctly - this may solve your ClassNotFoundError).
If it's a simple Java project, I would advise deleting it from the workspace, removing the .classpath and .project files and importing it again with the Create a Java project with existing sources wizard.
Either way, make a backup of your project before you start doing anything :-)

Eclipse plugin cannot find class from class folder

I'm developing an Eclipse plugin, and want to use some pre-compiled classes which I've put to lib/ folder in my project (so the directory structure looks like {proj_root}/lib/com/example/Required.class). Then I've added lib/ folder as class folder to project's build path. At build time all is working fine, I can import and use that classes in my code. And now the problem is, when I'm running my plugin in separate Eclipse instance I get ClassNotFoundException by my plugin refering to class from precompiled classes folder.I tried specifying my class folder in "Order and export" tab in Build path settings. Also I tried setting checkbox in plugin's build.properties GUI editor in front of lib/ folder to be included in binary build. In that case if I export my plugin to JAR I can see lib/ folder inside it, with my classfiles, but at runtime my plugin still cannot find any of classes from lib/.Can you please give me an advice on how I should tell Eclipse where to find that classes at runtime?
PS.:
I did research on stackoverflow on this problem, but all suggestions are the same: just add class folder in project's Build path.
At this point in plugin's build.properties Text editor, eclipse shows warning The class folder 'lib/' is not associated to any output library entry. and suggests to make this modification of code:
output.. = bin/,lib/. I have tried that solution but it is seems to change nothing with the problem.
It sounds very much like you have added the lib folder to the "Java Build Path" properties page of the plug-in project.
But... for plug-in projects, this does not work as these are OSGi bundles - you must include the lib folder in the PDE Editor on the "Runtime" page:

Wrong package name when using automatically added imports in Eclipse

I have several Maven projects converted into one Eclipse workspace. When I edit the code under Eclipse, I sometimes use CTRL+SHIFT+M combination of keys to automatically add imports to the classes from a project in my workspace. But somehow they are added like this:
import src.main.java.com.mycompany;
while to real package I wanted to import is com.mycompany.
This has to be some configuration in Eclipse to fix this but I'm confused. However, this issue is very annoying.
EDIT:
I've forgotten to mention that Eclipse files were generated using mvn eclipse:eclipse command.
Under Eclipse project seems to be configured properly. Its source folders set like this:
src/test/java
src/test/resources
src/main/java
src/main/resources
And everything under Eclipse works properly except the situation when I press CTRL+SHIFT+M keys
The standard source folder for Java projects is
./src
For imported maven projects, simply remove this folder from the list of source folders at the build path settigs. The correct source folder is
./src/main/java
In eclipse;
Remove the existing source folders first.
-right click -> menu -> build path -> remove from build path
then
Right click on the source folder.
build path -> use as source folder.
Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.
It's because eclipse is not aware of the convention over configuration filestructure Maven is following. Install the M2Eclipse plugin and File > New > Other > Maven Project for new projects or for existing ones right click on your imported project on Package Explorer > Maven > Enable Dependency Management. Once successfully done, on the Package Explorer you would see your project nicely gathered following the Maven conventonal filestructure like src/main/java, src/main/resources, src/test/java and from then on you'll start seeing your package structure hierarchy like com.mysite.myproject..
First remove it. Then add it back using right click on package->build path->configure build path-> Source->Add Folder and add the entire /src/main/java tree
You've got the wrong source folders in your build path, and it's a wonder that anything works at all.
You can use either the maven-eclipse-plugin or M2Eclipse to automate getting this right, or you can manually fix the build path to call our your actual source folders, not their great-grandparents.
Non of the above worked for me. Finally I just changed the name of the scr folder to scr-java and this removed the package structure with scr as root package.

Categories

Resources