Importing class/java files in Eclipse - java

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!

Related

Importing .java and .class file gone wrong in eclipse

Recently I worked on a project on a mac, using eclipse. When I was done with the project I copied certain .java files and their corresponding .class files onto a USB drive and brought it to my personal computer. When I try to use the import feature on eclipse and import everything, my main method is not recognized by eclipse. It will work if I create a new eclipse "class" by the name I have in the program and copy paste the code. I have many classes so I can not do this for each one. Is there any way for me to change all .java/.class files into files that will be read by the system?
Thanks
An eclipse project contains a lot of meta information. Just open the project directory in a file browser, and have a closer look. There are files like .classpath for example.
When you just copy your source code, you leave all the meta information behind!
You should either copy the complete project directory, or simply use the export task to create a ZIP file of your project. (to later import that in other systems).
There is one other way you can do this. Just create a new project with the SAME EXACT PROJECT NAME that you did at school or wherever. After you create your project, go to your file explorer and navigate to the following place:
<"Directory to work-space">/<"Project Name">/src/
For ex: in Windows, it is Generally:
C:/Users/your_name/workspace/projectName/src
In here, copy all your ".java" files.
Now go to eclipse, right click on your project and hit refresh. You will find all your .java files there. Right-click on your project and hit "Run"->"Run as a Java Project". It should run! hope this helps.
PS: You do not need your class if you are executing it in Eclipse. Eclipse will create its own class files.

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 :)

import .java file to Java project in Netbeans

I'm fresh to Java and cannot get this right.
I've got only a .java file from a Java study book, that represents a Java project. There seems to be no option to input such file from Netbeans interface, that would automatically compile corresponding class and enable project to run.
I've spend a long time to figure this out, copying files manually, changing the automatically created class names and packages, moving this around - but I don't find a solution.
If someone can help - this will be great.
Pawel
It seems the issue was following. .java file should be placed in "src" directory within the Project folder, rather than straight in the Project folder.
So if say "Project" is one's destination folder, then one can:
choose from menu: New Project-Java-Java project from existing resources - indicate path to the ...\Project folder
the next step is to choose "Source Packages Folders" and here the path should lead to ...\Project\src .If the file .java is correctly placed there, the creator will indicate below that .java file is included to the project
If the code is right after finishing the creator and compiling it we get a compiled class Project.class and we can run it :)
Not sure if there is some work around to this in NetBeans, as it's not super intuitive, but I might not have found a better option.
I too was looking for File > "Import File", because there is an "Import Project" option. It appears dragging and dropping the java file into Source Package is just as easy. :)

Importing source code of a library in Eclipse

I have a question about importing .java files.
I have downloaded source codes of a library that has many subfolders.
The way that I imported is to create a new folder within my project called libs and dragging dropping the entire folder, after that I right clicked into the libs folder and then clicked Build Path-> Use as Source Folder.
Now the issue is most package names are like "analysis.common.src.java.org.apache.lucene.analysis.ar" whereas within their source code their package name is referred as package org.apache.lucene.analysis.ar;
How can I fix this problem? Using CTRL + SHIFT + O does not fix it.
You can use Eclipse File System Import to import sources to a particular project. Here is a step by step explanation.
Another example. Scroll down to "Importing Files Into a Project" section

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

Categories

Resources