I cannot understand why in Eclipse after these two steps
Run as -> Maven clean
Project -> clean...
I can still run my program using green icon with white arrow?
I thought there are no .class files after cleaning. I was expecting exception like ClassNotFound, but nothing like this happened.
Do you know how to properly clean project in Eclipse?
Clean in eclipse is not the same as clean in maven.
Maven's clean indeed removes all compiled resources (classes etc).
Clean project in eclipse is a kind of mvn clean compile, i.e. it removes *class files and compiles all *.java files again.
I just checked this phenomenon out with one of my Maven projects in Eclipse and this is what I observed. By the way, don't trust what you see in the Package View, use your file system explorer.
Run as -> Maven clean : deletes the target folder
Project -> clean... : creates target folder with empty subfolders "classes" and "test-classes". In Eclipse's Package Explorer view, I see an empty target folder.
At this point I would say the proper way to clean a Maven project in Eclipse is simply to do Run as -> Maven clean.
Now to address why your program runs after cleaning. As soon as I ran a program in the project, Eclipse compiled and populated the "target/classes" and "target/test-classes" folders with *.class files and resources. In Eclipse's Package Explorer view, I still see an empty target folder.
The trick to figuring this out was to look at the project directory and sub directories with the system file explorer and not just with Eclipse's Package View or Project View.
In starting code for your application you can add:
System.out.println("Classpath=" + System.getProperty("java.class.path"));
you can analyze it (nornally I use notepaddpp to split it by classpath separator and sort) - maybe it refers to jars in maven repo which contains classes as well? Sometime class folders are first on classpath and thus are before jars even if in jars there are the same classes.
This is probably because eclipse will auto build your project when you run it
Related
I've been having a weird problem with eclipse.
When I compile my project via terminal with maven, it copies all the config files that I have inside my resources to the target dir, as expected.
But when I try to execute my JUnit tests inside Eclipse, it compiles the project again, but this time, doesn't copy the files to the designated target dir.
I tried running mvn eclipse:clean eclipse:eclipse on my project, to see if it helps and configures the project inside the IDE in a proper way, but still have the same behavior.
Does anyone had this behavior before? I couldn't find something related, so far.
I have an existing maven project which I downloaded from GitHub.
If I run mvn clean install from the root of the project everything works fine (a jar file is created in target folder and it executes works OK).
But when I go to Eclipse and import this project as Existing maven project I get multiple (395 to be precise) Java Problem errors (like The method is undefined etc.).
The only thing that I notice is when I run maven from command line it creates .m2 folder inside the project folder while maven from Eclipse creates .m2 inside my C:\Users\username folder.
I'm using Eclipse 2019-06, Java 8 and apache-maven-3.6.1.
P.S. Same errors appear when I import this project to Intellij IDEA 2019.2.
Olga, don't worry. Import the project into Eclipse. Then wait some minutes. If the errors do not vanish, try "ALT + F5". If this still does not help, build the project from within Eclipse ("Run As -> Maven build"), do "ALT + F5" again.
Eclipse often takes some time to fully "understand" the project, i.e. synchronise the POM and the files to its internal mechanisms.
I have a fundamental question, but something which bothers me a lot. We sync code from perforce in my company. and then we are taught to build the code compatible to eclipse. Then we import the code in eclipse as existing project. Then when we hit ctrl-space from an object, we get suggestions of methods.
My question is: Why do we need to build the code for suggestions of methods? . After syncing from perforce, what I have is still an existing java project (right?). So eclipse should be smart enough to be able to index and find out from source code, the methods, when I hit ctrl-space on an object?.
Why do I need to build? What purpose the build serves?
The code does not have to be built, but to be able to import a directory as project via File > Import...: General > Existing Projects into Workspace into Eclipse, at least the file .project must exist.
There are several ways to import, for example, a Maven project:
Execute mvn eclipse:eclipse on the command line and in Eclipse do File > Import...: General > Existing Projects into Workspace
File > Import...: Maven > Existing Maven Projects without a command line call uses by default the in Eclipse embedded Maven
File > Open Projects from File System... (or in Git Repositories view right-click + Import Projects...) detects not only Maven projects but also plain Java projects and automatically configures them accordingly.
In all three ways the following files are created, which are required in Eclipse for the Java content assist (Ctrl+Space) to work:
.project - says whether it is e. g. a Java, a PHP or a C/C++ project (to be more precise, the project name, builders and natures are specified here, e. g. the project folder icon of a Java Maven project will be decorated with a M and a J based on the Java and Maven project natures)
.classpath - tells Eclipse where the source and output folders are and which JARs should be added to the classpath (in Maven projects, the JARs are not listed, but it refers to the Maven dependencies which are computed from the pom.xml file)
.settings/org.eclipse.jdt.core.prefs - contains Java compiler and optional formatter settings (e. g. which Java version; which problems should be ignored or shown as infos, as warnings or as errors, etc.)
With Eclipse Oomph you can automate even more: you select a project and based on a project-specific configuration, for example, a Git repository is automatically cloned and plug-ins necessary to edit the project are automatically installed and configured.
Eclipse uses relfection for the suggestion, it can be possible only if the .class file generated
I have been developing in NetBeans, I am exploring changing over to eclipse.
We have a set of directories for the modules in our project; we have scripts that check things out of Subversion and do other things with that directory tree, and we have NetBeans access the sources, etc., from that directory tree.
All our modules are built, etc., with Maven. All of this works fine from within NetBeans.
I have successfully imported all the projects into eclipse (File / Import / Maven), but evidently something is different in the handling of "generated sources". We have some castor-generated files in one module, and jaxb-generated files in another. The generated files in both cases are not found by the compiler, and therefore these modules have compile errors.
Maven is configured to generate the Java classes which cannot be found; maven clean deletes them and maven compiler:compile puts them back. But in eclipse those java files cannot be found for compiling other classes, but in NetBeans they can.
I tried deleting the generated java files, then rebuilding in eclipse; the java files were regenerated in generated-sources, but still cannot be found by the compiler when it comes time to compile (or syntax-highlight) my code.
It sort of looks like a classpath issue, but we don't set the classpath in NetBeans for a maven project. How should I configure things in eclipse so the generated class files are found?
In Eclipse in the Package Explorer right click the project folder which contains the missing files. Goto "Build Path >" then "Configure Build Path...". Properties for your project folder will open.
Click on the "Source" tab. Then click on the "Add Folder..." on the left. A source folder selector box opens, here select your folder which contains the auto generated sources. Normally it will be in the target directory. Then press ok. If this will not work, try one directory deeper or less deep.
If there is no target directory, build the project folder with maven on a shell.
The generated-sources directory will be stored in the .classpath file in your project folder.
HTH.
How to export java project to JAR with Netbeans ? I cannot find options like in Eclipse.
You need to enable the option
Project Properties -> Build -> Packaging -> Build JAR after compiling
(but this is enabled by default)
Do you mean compile it to JAR? NetBeans does that automatically, just do "clean and build" and look in the "dist" subdirectory of your project. There will be the JAR with "lib" folder containing the required libraries. These JAR + lib are enough to run the application.
If you disable "Compile on save" in the project properties, then it is no longer necessary to do "clean and build", simply "build" will suffice in most cases. This will save time if you want to change just a bit of the code and rebuild the JAR. However, note that NetBeans sometimes fails to handle dependencies and binary compatibility properly, which will lead to a faulty JAR throwing "no such method" or other obscure exceptions. Therefore, if you made a lot of changes since the last full rebuild and even remotely unsure that it will still work even if some classes aren't recompiled, then you must still do a full "clean and build" in order to get a perfectly working JAR.
It does this by default, you just need to look into the project's /dist folder.
Right click your project folder.
Select Properties.
Expand Build option.
Select Packaging.
Now Clean and Build your project (Shift +F11).
jar file will be created at your_project_folder\dist folder.