Adding .java files into eclipse? - java

I am so new to this, this is probably a stupid question but...
Basically my lecturer has given us a bunch a of .java demos. They are all inside a single file called 'demos'. I was wondering how would I import the 'demos' folder into Eclipse so I can use the folder expand it and select and drag any program I want to run into the work space.

Right click in project, Import... > General > Existing Projects into Workspace, chose your project.
Or:
Create a new project, drag and drop example.java files.

Extract your demos folder somewhere. Right click on the project browser, then:
New -> Java Project
Enter a project name then press Next
Click "Link additional source folder" in the bottom half of this page
Select your source folder
Finish
Now your folder will appear in your project, and you can edit files as usual. Keep in mind that editing / adding / removing files there will modify the actual files in that source folder.

Related

What is the best way to rearrange files in a big project in Intellij?

I am new to IntelliJ IDE so i might ask silly question.
I am using IntelliJ IDE and working on a big project. I want to change the name of 1 directory(A to B) and want to move few files of this directory in to another directory(C), what is the best way of doing it so that I don't need to change the package name inside .java file(i.e IDE handle all those things)?
Right click the directory in the project explorer ->refactor->rename A to B.
Then drag and drop the files you want to move into the C package using the project explorer.
Intellij will automatically change the package definitions at the top of the .java files for you.
Or use shift+F6 for renaming directory.
For moving files between directories
Just select file (If you want to move multiple file , select files by pressing ctrl + select) and move to destination directory

How to import OEM Java lib and source into Netbeans?

I have done a reasonable amount of programming in various languages but I am a Java / Netbeans newbie. I have successfully created and run "Hello World" so my Netbeans installation is OK.
I want to write code to control an LED display controller card. I have sample demo code zipped up by the OEM. I am trying to import, run and edit that code. If I can get this running, this will be the starting point of my own code.
The zip files only include library and source files in a directory structure that does not match Netbeans. All of my attempts to import the source and lib files have given fatal errors in Netbeans.
The directory tree has a lib and src branch at the root. The src branch cascades 4 levels to get to the source files.
I figure if I use a different tree structure, I will need to change the import statements in the source file. I'd rather not touch the code until I have it running. I have tried to unzip the project tree directly into the Netbeans directory but that didn't work.
How to import a JAR (Java Archive, the "library" in your case) in Netbeans:
Right click on your project name, in the 'Projects' pane usually located on the left
Click the 'Properties' link from the pop-up menu
Click the 'Libraries' link in the Categories list
Choose 'Add JAR/Folder', browse to your JAR and click OK
If you want Netbeans to create the JAR for the sources you have, do like Stefano says.
You should package the src folder as src.zip with a zip utility and the structure under lib with the java archiver as a jar file.
Now you can define a library in NetBeans and add these files to it (Tools => Libraries => New Library).
For Netbeans to be able to open a project, the folder has to contain some files that Netbeans itself generates when a project is created.
Since you only have src and lib folders available, I think the easiest solution would be to create a new project from Netbeans, so that the creation of those files is automatically managed, and then copying the content of the folders you have in the folders Netbeans will create.
You will also have to tell Netbeans to use those library, and you can do that from you new project's preferences.
Edit
A few more things you can try:
in NB, right click the folder containing the JARs you imported and select "remove", then import them back but selecting the jar files instead of the whole folder;
if you're not already doing so, use the "Clean and build project" button instead of "Build project";
If none of the above solve the issue, try starting from scratch again following these steps:
unzip your OEM files somewhere in your disk;
create a new empty project in NB
copy the files from the src OEM subfolder to src NB project subfolder
in NB, right click Libraries, then add JAR/Folder and select the jar files from the lib OEM subfolder
If this doesn't work either, tell me where I can find the OEM stuff and I'll try it myself :)

Importing class/java files in Eclipse

I'm new to Java Programming using Eclipse, so would really appreciate your help for this question.
I have doing Java programming both at college and home. At home, I have all my Java classes under a single project-(Java Reference). Now, in college, the java programs are compiled on the lab machines. And I would really like to have all these programs in a single project, on my laptop.
I had recently copied all the .class and .java files from the programs we did in college labs, so I could have them under the "Java Reference" project in my laptop. I copied the .class and .java files under the "bin" and "src" folders of the java project, on my laptop. However, I don't see anyway to import them through Eclipse.
From what I read here, it looks like having an entire project(created somewhere else) could be copied into one place, through the import option, but in my case, I only have the .class and .java files.
First, you don't need the .class files if they are compiled from your .java classes.
To import your files, you need to create an empty Java project. They you either import them one by one (New -> File -> Advanced -> Link file) or directly copy them into their corresponding folder/package and refresh the project.
You can import a bunch of .java files to your existing project without creating a new project. Here are the steps:
Right-click on the Default Package in the Project Manager pane
underneath your project and choose Import
An Import Wizard window will display. Choose File system and select the Next button
You are now prompted to choose a file
Simply browse your folder with .java files in it
Select desired .java files
Click on Finish to finish the import wizard
Check the following webpage for more information:
http://people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html
create a new java project in Eclipse and copy .java files to its src directory, if you don't know where those source files should be placed, right click on the root of the project and choose new->class to create a test class and see where its .java file is placed, then put other files with it, in the same directory, you may have to adjust the package in those source files according to the new project directory structure.
if you use external libraries in your code, you have two options: either copy / download jar files or use maven if you use maven you'll have to create the project at maven project in the first place, creating java projects as maven projects are the way to go anyway but that's for another post...
I had the same problem. But What I did is I imported the .java files and then I went to Search->File-> and then changed the package name to whatever package it should belong in this way I fixed a lot of java files which otherwise would require to go to every file and change them manually.
import class folder does not work for me, but add jar worked!
1. put the class folder under the project folder
2. Zip the class folder
3. Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars".
4. Add the zip file. Done!

creating multiple src directories in eclipse

I am doing this tutorial, which asks me to create multiple source directories in eclipse. Can someone explain how to do that? I am describing the steps I have taken so far as follows:
The image below shows the tutorial's directory structure on left, and my current directory structure on right:
As you can see, I need to create the following folders under Java Resources:
src/test/java
src/test/resources
src/main/resources
However, when I right click on the Java Resources folder and select new source folder, I get the following dialog box, for which I have clicked on the Browse... Folder Name button to open the subdialog which is also shown:
At this point, none of the choices seem to be what I want. So how do I create the three new src subdirectories that are specified by the tutorial?
Right-click your project, select Build Path and Configure Build Path.... In the Source tab, if src/main/resources or src/test/java appear, remove them. This might be a bug with the Maven plugin, I don't know. They appear like they are there, but aren't really.
Then use Add Folder... to add the folders you need. Do this by selecting a folder (to add folders to), like src/main, clicking Create New Folder... and using the folder name resources (or as appropriate).
You close the "Existing Folder Selection" dialog (since you don't want an existing folder, but a new one), and you enter src/test/java in "Folder Name". Then you click Finish. You repeat this operation for every new folder you want.
Of course, you could also use your file manager to create the folders, and select them in Eclipse.

Linking external source folder with name "src" in an eclipse project

This is NOT a question about linking source in eclipse java project
Is it possible to add an external folder named "src" in eclipse as the source folder.
The problem here is to keep the external folder name as "src" and not any other name; to do that I tried deleting src and bin folder from the standard eclipse java project and then tried to "Link Source" but that does not work.
Is there any other way I can make this happen?
That is make the Eclipse java project's src actually point to an external folder named "src"?
Similar issues with the out put folder "bin" as well.
System Information
OS: Windows 8, 32 bit
Eclipse: 3.7
Thanks.
You need to use Eclipse to remove the existing source folder from the project configuration, then delete it from the file system, then you can add the externally linked source folder as "src".
Right-click on the project and bring up "Properties..."
Click on "Java Build Path" and then the "Source" tab to show a list of the source folders on the build path.
Select "yourproject/src" and click the "Remove" button. This will remove it from the project (and the .classpath file).
Come out of it and physically delete the folder.
Go back to that dialog box and this time click "Link source...". It should work now. It works for me.
To make link with external folder, we need to rename the folder if any folder already exists with same name like "src" or better delete that folder. After that you can make link with the external folder using the anyone of the following method:
Method: 1 - (Make link manually via code):
1. Open the .project file from root folder of your workspace.
2. Include the below code with your file path:
<linkedResources>
<link>
<name>folder_name_list_in_your_project</name>
<type>2</type>
<location>folder_path_to_include</location>
</link>
</linkedResources>
3. Save the file and refresh the project to include the external folder in your project.
Method: 2 - (Make link manually via UI):
1. Right click the project, select "Build Path -> Configure Build Path".
2. Choose "Source" tab.
3. Click "Link Source" button.
4. Click "Browse" to choose the folder.
5. Enter the folder name in "Folder name" field to list the external name in your project.
6. If you need to add the pattern for include and/or exclude file, click "Next" and enter the pattern. Then click "Finish".
7. Otherwise click "Finish".
It works for me and hope that my steps also help you.
It is somewhat ugly, but you can create a hardlink in your filesystem. In the Windows command line, use mklink /H src d:\path\to\external\src.
I know that the following will hardly be useful, but: Think of a different approach.

Categories

Resources