add folder of .java files to intellij project - java

I'm trying to add http://www.json.org/java/ to my java project as a module on IntelliJ. I cloned the source code from the git repo on this link, and I put the folder with all of the component .java files on my desktop.
When I try to add this as a module in Intellij, it allows me to, and the files show up but I cannot import them or do anything with them. Can someone shed some light on the proper procedure on how to attach these .java files to my project?

Probably it is because the .java files are not recognized as source files but regarded as any file.
I suggest, you create a module in IntelliJ first, and move these files to the source directory of the new module.
Create a new module by clicking File -> New -> Module -> Java
Give it a name such as json-java
Clone from git repo directly into the new module's source folder. (json-java/src by default). IntelliJ should now recognize them properly as source files.
This should allow your java project to import the .java files.

Related

Can't find the classes of an external jar imported in Eclipse

I created a new project in Eclipse in which I want to use the JTS library.
I imported the library during the creation of the project but I can't find the classes in the package explorer.
Since I am not experienced in Java and Eclipse, I'll explain what I did from the start.
I download the JTS library from GitHub: https://github.com/locationtech/jts
I create jts-core.jar using jar in the command line. I place it in workspace/Libraries
In Eclipse I create a new project in workspace/Projects/JTS-test. In the "build settings" I go to "Libraries" and "Add external JARs..." and select my jar.
This is what my package explorer in Eclipse looks like after step 3 :
However, when I browse in jts-core.jar outside Eclipse, org/locationtech/jts/algorithm/ is full of .java files and contains directories themselves containing other .java files.
What did I do wrong?
The JAR file referenced as a library must contain the *.class files (bytecode). In the JAR, the *.java files (source code) are only to see the source code and the Javadoc, but are not required.
Before jar, you missed compiling the *.java files (javac). Instead of creating a JAR yourself, you can download a ZIP file with all required JARs from the release page of the jts project. This ZIP file contains also the JARs that are required by jts.
Alternatively, you can copy the *.java files into the source folder (src by default) of your project or create add a new source folder for these *.java files. In contrast to classes in JARs, *.java files in source folders can be edited.

Java Project Environment Preparation

I am new to project environment setup. Below is my project structure in eclipse
Project Name
--> .settings
--> .bin
--> lib
--> resources
--> src
--> .classpath
--> .project
I am attempting to export src folder as jar.
When i export to jar, all the above folders & files are created in jar. But i need to convert only src folder as.
Also when i export to executable jar, all the third party libraries are exported as class files in jar. is it right.
What is the best practice to export project. Only src folder or everything.
Which i need to use jar/runnable jar. My requirement is to write start/stop bat file to call jar and execute java program.
Please advice me. Thanks in advance.
First it's important to know what these folders actually do. Following are the workings of several of these files.
.settings -> This file records project specific settings and workspace preferences.
.bin -> folder is usually where the compiled files are copied to.
lib -> contains external libraries that are used in your project (like Apache Commons)
resources -> the resources like images, text, pdf, audio, video are usually copied here
src -> the folder where the project's source files are located.
.classpath -> It contains information that the JDT feature needs in order to properly compile the project: the project's source folders, the output folders , and classpath entries.
.project -> This file is maintained by the core Eclipse platform, and its goal is to describe the project from a generic, plugin-independent Eclipse view.
So you can see that if you exclude some of the files like lib, resources, bin etc... Your jar file will probably stop working. Your jar file needs compiled files and their dependencies.
For example: All your compiled .class files are in bin folder. And your jar works because of these .class files and NOT .java files that are in src. If you delete this bin folder then your jar will probably stop working.
Also, your project may be using some external library supplied by someone else. Like Apache Commons or google/guava and these are usually in lib folder. So you can't delete this folder as well.
However, if you no longer expect to use .java code, then you can exclude files that were created by eclipse to manage this project. See this post.
see also:
1. What's in an Eclipse .classpath/.project file?
2. exclude files from jar or war in eclipse

Creating a source package folder in a CounterClockWise project programmatically

I have an existing java project and in that project there is a procedure that should programmatically create packages with source files inside them. It uses the standard File.mkdir() method to create all the unexisting directories for the package and then creates and writes the source files in there.
All works fine except that only the top folder created for a package path is marked by Eclipse as a package. The lower level folders remain just folders. For example, if I create a directory structure for package "org.prj.hey.this.is.cool", where "org.prj" is the existing path for my main project package, it then creates the rest of the path "/hey/this/is/cool" and only "hey" becomes an Eclipse package. I have found no way to make Eclipse recognize the "this", "is" and "cool" folders as packages. I haven't found any clues on this in the .project and .classpath files either..
So how do I make the whole branch of created folders be recognized by Eclipse as packages?
UPDATE:
(screenshot)
Found out that the issue is due to the file extension (see the picture). Is there any way to make Eclipse recognize .clj files as source files?
Problem exists in your current eclipse workspace buildpath configuration. Perform rightclick on your project name, then "Build path->configure build path..." and add newly created dirs to your current build path in SOURCE tab.

Import project (jar) into Eclipse

I got a .jar file which I need to import into Eclipse. However, I don't want to have the jar as a referenced library. I need the .jar to be included like a "normal" project, with packages(!) and .java files.
I tried to do the following:
New Java project -> Import -> General -> Archive File. In this case, when I place this jar also as a referenced library, it is imported but with .class files.
New Java Project -> Import -> General -> File System. Imports the .java files, but the packages are lost and are normal folders. Also, the files are somehow strange, because the "j" in the icon looks differently, and errors are not noticed (no underlining)
Importing "Existing Projects into Workspace" doesn't work at all, it says that there is no project. I also tried to import the jar as a zip after extracting it, this gives me the -java files, but it destroys the packages.
Does anyone know how to import this correctly?
I have managed it this way:
New Java Project -> Java settings -> Source -> Link source (Source folder). There I added my decompiled jar and it was imported correctly :)
You can also create a new Java Project and then do File -> Import -> General -> Archive File. This will save you a step of unzipping your jar and adding as a linked source folder.
You cannot import a jar that way, unless it has the source code packed in the jar. If you really need the source code, you'll have to find an archive somewhere. But of course, not everyone is willing to share his source...
I tried the below and it worked.
Create a New Java Project
Goto File > Import > General > Archive File
Select the required Archive file(.jar in this case) from your local system.
Select the project you created in Step 1 and click Ok
This will save you a step of unzipping your jar but if there are .class files, eclipse won't convert them to .java files. This has to be done seperately.
Below helps in all recent eclipse.
Please try below eclipse plugin to import jar as project :
Step 1) Eclipse Help > Eclipse Marketplace
Step 2) Install the plugin "Import Jar As Project"
Step 3) Restart Eclipse
Step 4) File->Import->Other->Jar without source
Step 5) Select a jar/war file and click finish
New project will be created from the jar
https://marketplace.eclipse.org/content/import-jar-project
Thank You.

Can you import a non-archive project directory in Eclipse?

I read this article which shows how to import/export projects in Eclipse (although it seems a little outdated and I'm using 3.7 Indigo).
To export a project, you go to File >> Export and it creates some kind of "project archive" file on the local file system. You could then email it to someone else, who could then import the archive as a new project or into an existing project.
But what if you don't have an "archive" file to begin with?!? What if you just have the project files and directories but without all the Eclipse-metatdata (.project file, etc.)? Is there anyway to tell Eclipse to look at, say:
/home/myuser/some/path/to/project/root/
src/main/java
SomeObject.java
build.xml
...and get it to read that as a new Java project?
Try the steps below:
Create a Java Project in Eclipse as below:
Load the project contents from your file system as below:
Hope this works for you.
.project and .classpath files are the base of the project structure. Without a .project file Eclipse won't recognize your project. The .classpath is important too but that one changes between environments. Should you use the files from another environment you'll have to reconfigure some settings.
Rather than emailing code, I suggest you use a Version Control system and, also, some Eclipse plugins. SVN + Subclipse for example.
If you trully want to send the project to another person just send him the project's folder and import it as an existing project by doing right click on the project explorer and selecting Import > Existing Project into workspace.

Categories