Importing a Log4j source in eclipse - java

I am trying to import the latest Log4j2 source in my eclipse and Eclipse is not detecting it as a project. it says 'No projects are found to import'. Is there a solution for this?
My goal is to make a jar out of this source. Can i do that without the need to importing it on my eclipse(maybe like a command line solution that can make a build for me)?

Eclipse is not detecting it as a project. it says 'No projects are found to import'. Is there a solution for this?
It's a maven project, so you should import Existing Maven Project
Can i do that without the need to importing it on my eclipse(maybe like a command line solution that can make a build for me)?
A simple jar command. You can see more at Using JAR Files: The Basics. A basic command example
> jar cf log4j-sources.jar * // the * is for all files in the current directory
Note: this will not build anything. Just put everything into a jar.

If you have maven you can ask maven to generate the Eclipse .project and .classpath files.
From the log4j2 top-level folder, run mvn eclipse:eclipse
Eclipse will now be able to recognize each module as a project.
After this, to import modules in Eclipse, take the following steps for each module:
File > New > Java Project - enter module name (e.g. log4j-api) for Project name, uncheck "Use default location" and click Browse... to navigate to the location of the log4j-api module. Then click Finish on the wizard.

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.

"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 doesn't see java file generated during installation of maven module

I am working on a specific maven module. I have it imported into eclipse by m2eclipse plugin. The module uses our internal plugin to generate java files from some csv files. During install goal those files are being generated and put to target/generated-sources/folder_name.
Can you tell me how I can import these java files into eclipse so it stops showing me compilation errors saying that 'x' cannot be resolved to a type?
Now every module which uses those files has compilations errors in eclipse, everything works fine when I try to build from command line.
Thanks in advance.
UPDATE: Thanks a lot guys, I was trying similar solution but instead of adding source folder I was trying to add external classes :|
project -> properties -> java build path -> source -> add folder -> (search for target/generated-sources/folder_name, create it if deleted by maven) -> ok
now eclipse will only complain when you'll use mvn clean deleting target folder.
Eclipse refreshes file content on request, so any change made external to eclipse, eclipse doesn't know about that
You can make eclipse to use native hooks to detect external changes
preference > workspace > check refresh using native hooks
and add that directory to source path
You probably need to add the folder of the generated files as a Source folder in the project. Look in the Project Properties > Java Build Path > Source tab and add the folder.

How to import and run ivy+ant project in eclipse juno

this is my first use of ivy+ant and after building the project from command line, i want to import it into eclipse juno.
the project folder (trunk) contains the following folders/files:
build
dist
src
WebContent
build.properties
build.xml
ivy.settings.xml
ivy.xml
i tried to import the trunk folder that contains the above files/folder using import existing projects into workspace, but i get the error that no projects found.
pleases advise how to import this project.
UPDATE: i tried to configure the project for eclipse before importing using the following command:
ant eclipse-default
but i get the following error:
Target "eclipse-default" does not exist in the project
UPDATE 2:
i was able to import the project in eclipse as follows
File > New > Other > Java Project From Existing Ant Buildfile
but when trying to run the project on server, i can't find run on server in the run options, please advise.
You may try this:
Install IvyDE from http://www.apache.org/dist/ant/ivyde/updatesite (ref: http://ant.apache.org/ivy/ivyde/download.cgi)
IvyDE may not recognize your setting and properties file. At Window -> Preference -> Ivy -> Settings, check reload the setting on demand, add Ivy setting path ivysettings.xml or your Ivy setting file, Property files build.properties or your Ant properties file.
Add new Java project you want to work on, point the path to your project.
At the new project wizard, Libraries tab -> Add Library -> IvyDE Managed Dependencies.
You can not import a project of you do not have the eclipse project information in your code.
Either create a new eclipse project from existing source.
Or a new java project from ant buildfile (your update 2).
For running need configuration of server (hard to know what youneed there), you may need to create new server (preference/server/run time environnement) (or from view server, right click "new")

How to create a jar with external libraries included in Eclipse?

I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
Personally,
None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.
If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,
Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.
If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.
Or
If you have a web project then put all the jars in web-inf/lib folder and do the same step.
Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library. Then try to export as a runnable jar.

Categories

Resources