Adding .class files to an Eclipse project - java

This is a total newbie question, but I can't figure it out so I figured that I would ask it here and see what happened.
Here's the problem: For my java programming class, we are supposed to download a .class file created by our instructors containing a custom-made class with methods that we are supposed to use in an assignment. I already know all of the code to create the class within my actual program, but I can't figure out which directory to place the .class file in. I have tried the src and bin folders inside the project directory, and even placed it directly into the project directory, but nothing seems to work. My instructors say to just put it in the same directory as my java program, but they are using jGRASP. What am I doing wrong?

You can place the class file anywhere, just be sure to add its directory to your project's build path. Right click on the project, then properties at the bottom then build path, til you find add class folder.

You have to create a project, or open your project if you have it already, then, while you are in the Java view, select File > New > File. It will open a window, you can create your file there and choose in which project folder do you want to put it. If in that window you press the button "Advanced >>" it will give you the option to "Link to file in the system", check it, and then you can browse your folders to the downloaded file.
I don't know if this solves your problem, it's what I understood from your question.

Your instructors class file must be in the build-time and the run-time class paths for your project to use it. Instructions assume that you are on a PC.
Run-time class path setup
Create a directory (perhaps C:\JavaRoot\classes). I will refer to this as the Classes directory.
If one does not already exist on your PC, create an environment variable named CLASSPATH
Add the Classes directory to your CLASSPATH envoronment variable
Download the class in question and put it in the Classes directory.
Build-time class path setup
Right mouse on your project in eclipse.
Select "Properties".
Select "Java Build Path".
Select the "Libraries" tab.
Click the "Add a Class Folder" button.
Browse to and select the Classes directory you created during "Run-time class path setup" above.
Click the "OK" button

For Eclipse use the following:
Project > Properties > Java Build Path > Libraries (Tab) > Add External Class Folder (Button)

If you have a configured project in Eclipse and you manually added a Java class file in some of the project directories, you just need to refresh the project for Eclipse to recognize it:
Right click in your project (in "Project Explorer" view) -> Refresh

Related

Add a .class file to a project in NetBeans

Our professor provided a .class file (Out.class) we should use in our Programm. When I create a new project in NetBeans, in what directory am I supposed to copy that file and/or how do I "import" it to my project so I can use it like Out.methodwhatever(var);?
I have already searched for that problem, but I just found cases where people have a .jar file, but I have a .class file.
Copy the Out.class into some directory (not your source folder). Make sure you replicate the package structure of that class, e.g. if that class is actually com.foobar.Out you need to put it into e.g. libs/com/foobar
The right click on your project in NetBeans, select the "Libraries" category, and click on the "Add JAR/Folder". The select the e.g. the libs folder from the above example.

how to create jar file using eclipse

I have a folder name called configurator which has many sub folders and class files. I need to create a configurator.jar file of this folder using eclipse.
I followed below steps to create jar file.
create a java project
right click on project name, go to properties, go to java build path, click on source --> link source, browse and select configurator folder, add required external libraries --> click ok.
Now, I am able to see the folder structure in navigator. Right click on configurator folder and select export
select jar file and save the jar file in specified folder.
But, after saving the jar file, i am not able to see the class files inside the jar.
My question is: Why the class files not exported into the jar? How to do it using eclipse?
Creating a JAR file using Eclipse IDE:
Right click on your project, which you want to create a JAR file of. And select Export from the context menu.
Select JAR file from Java folder and click Next.
In eclipse Latest version Runnable JAR file is added.
Provide the Destination path and click on Finish to create the JAR.
Select Particular project, right click->Export
Under Java select JAR file, click on Next
(Don`t change any properties) select Jar file location where you want to save and click on Next
It will show JAR Package Options. Don`t make any changes, click on Next
Finally, select Main Class location. Select your particular class and then finish
Now your jar will be created in given location
Remember in Eclipse to run once your java class(as java application), and then you can export it to runnable jar (it will show in lunch configuration).
JAR files can be created using following method i have told
Example:
step1: Create One project and name it as a Project1
step2: Create another project and name it as a Project2
step3: Create packages for two projects
step4: Create classes for projects
step5: Right Click on Project1 go to export in that select Java go to jar files
select Project1 then click ok and ok.
step6: Right Click on Project2 create one folder in that u have copy the jar file what we have created for project1.
step7: Right click on project2 go to properties select buildpath then click on libraries then click ok button then ok.
step 8: then type import package to the Project2
Your jar file is ready.

I am getting resource not on the buildpath of the project [duplicate]

This question already has answers here:
Eclipse: The resource is not on the build path of a Java project
(16 answers)
Closed 5 years ago.
I am getting The resource is not on the buildpath of the project when I am trying to "Organize imports" or trying to access any class properties.
I am building my project with ant.I tried to configure my buildpath in javabuilpath -> Projects but it is not showing me my src folder there.I am not able to use quick assist (ctrl + space) and class related properties because of the same reason
and it does not show me any syntax errors also while i am writing my code.
please help me.
thanks
I just hit this problem myself, and it's baffling. You get it in a bunch of different places as you try and treat a Java file as, you know, a Java file but Eclipse hasn't figured out that it's actually a class within your project. The reason it doesn't know (or the reason it didn't know in my case) was because the directory hasn't been marked as a source directory for the project. So you need to tell the project configuration that the file is in a directory that holds source files. Here's how:
Open your project's properties.
Under "Java Build Path", click on the "Source" tab.
Click "Add Folder..."
Find the folder where you have the unrecognized java file. (In my
case it was test->Java)
Put a check by the folder. (I put it only by "java" and not by
"test" given that the test folder holds a lot of other stuff.)
Click ok, click OK again, perhaps refresh, and you should be good to
go.
Your src folder shouldn't appear in the Java Build Path -> Projects tab. Rather, it should appear under Source tab.
Try imorting the class files as external jar.
What icon is used for the file? If it is an outline J eclipse doesn't recognize the file as being a source file of the project.
If so, you need to declare the parent folder of your package a source folder, i.e. if the path looks like ".../src/my/package/MyClass.java" the "src" must be declared a source folder (right click -> Build Path -> Use as Source Folder)

How to take package .java files into a Eclipse Project

I've got a copy of a java package, with example implementation. The package is in a structure called com.java.project (folders, containing myriads of .java files) and there's also a few example files (example1.java, example2.java)
The example files have 'import com.java.project' calls in them, that's all well and good. However, I can't seem to work out how to get Eclipse finding the package contents (currently returns "Type not found" for any calls to the project objects.
I've tried placing the com structure in the same level as my example1.java, but that doesn't work. I have read that I need to compile the package into a .jar to get it to work w/Eclipse, but that doesn't seem to want to behave, none of the files compile.
In Eclipse you can right click on the Project Explorer and select "New -> Java Project"
Then you can import your java files by right clicking your project, selecting import from file system. If the class file shows an error, open the class file, the class name should have red curly braces, click on the error tick mark and Eclipse will give you an option to move the file to the correct package which in turn creates the folder structure for you.
1.Can you do a check on the build path of your Java project? (Right-click the project -> Properties -> Java Build Path -> Source tab), and see if the root folder contains the code is properly specified here.
2.Maybe a Project->Clean will help as well.
Hii,
Just Create a new project with File -> New menu
you need to open Java Perspective and there you will find a package named "src" you need to copy your "com" directory and paste it at the src folder.
I hope this will work for you.

How to create a project from existing source in Eclipse and then find it?

I have created several .java files. All of them are located in one directory. I used a text editor to write these files. Now I want to switch to Eclipse. How can I do it? I have tried many ways. None of them works.
ADDED:
I think the common way is to have source in the eclipse folder called "workspace". So, how do I get my files in these directory. Should I use eclipse to create a new project from existing source and Eclipse will put all file to the workspace? Or I should manually copy all my files to the workspace? Where should I put my class files than? Should I create a subdirectory? With which name?
ADDED 2:
When I try to create a project with name "game", the eclipse writes me that a project with such name already exist. But how can I open this project?
ADDED 3:
In my "workspace" I have created a subdirectory called "game". I copied all my .java file into this subdirectory. Then with Eclipse I created a new project with the name game. As a result, Eclipse created .classpath and .project files in the directory "game". It also created bin and src subdirectories. And now I think it is not the correct way to go. The source files are supposed to be in the "src" directory. Right? And at the moment all my .java files are in the "workspace\game".
ADDED 4:
I did it other way around. With Eclipse I have created a new project with the name "game". As the result, Eclipse created a folder called "game" into folder "workspace". In "game" the folder I found "src" folder. I copied all my .java files into this folder. But now in the "Package Explorer" I cannot open "src" folder. So, how can I access my source files from Eclipse? Why Eclipse does not want to open the "src" folder?
Easiest Method:
Put all source files into one directory named after your project. i.e. "ProjectName" You can keep this directory in your workspace or it can be somewhere else.
Start a new project in eclipse and name it using that same project name.
Uncheck the "use default location" box and find the directory where your project is unless your project is already in the workspace - then you must not uncheck the "use default location" box
Click 'next'.
Eclipse should be smart enough to figure out what's going on. After clicking next, it will show you all of the files it found in that directory. It will just automatically add those files to your project. VoilĂ !
Right-click in the package explorer and select New - Java Project
Create the new project Game
Open the new project in the package explorer - you should see only the source folder called src (there's nothing inside yet)
Open a file Explorer (e.g. Windows Explorer) and drag your sources
Drag them to Eclipse and drop them inside the new src folder - if asked select "Copy files"
Eclipse should put the files into the default package, if that's not correct you can edit the offending files (marked with a red cross) by opening them in Eclipse, selecting the package declaration (usually line 1), pressing Ctrl + 1 and selecting the appropriate option (e.g. "Move xy to package com.game"
This answer is going to be for the question
How to create a new eclipse project and add a folder or a new package into the project,
or how to build a new project for existing java files.
Create a new project from the menu
File->New-> Java Project
If you are going to add a new pakcage, then create the same package name here by
File->New-> Package
Click the name of the package in project navigator, and right click, and import...
Import->General->File system (choose your file or package)
this worked for me I hope it helps others.
Thank you.
The easiest method is really good but you don't get a standard Java project, i.e., the .java and .class files separated in different folders.
To get this very easily:
Create a folder called "ProjectName" on the workspace of Eclipse.
Copy or move your folder with the .java files to the "ProjectName" folder.
Create a new Java Project called "ProjectName" (with the Use default location marked).
Press <Enter> and that's it.
There are two things
1- If its already a Eclipse Project, then simply go to File->Import->General->Existing Project into Workplace
2- Otherwise define project type e.g. Java, Web etc
Create a new project of type you define into your workplace. Copy Paste source , lib and other necessary files. refresh, compile and run project in eclipse.
In the package explorer and the navigation screen you should now see the project you created. Note that eclipse will not copy your files, it will just allow you to use the existing source and edit it from eclipse.
There are several ways to add files to an existing Java project in Eclipse. So lets assume you have already created the Java project in Eclipse (e.g. using File -> New -> Project... - and select Java project).
To get Java files into the new project you can do any of the following. Note that there are other ways as well. The sequence is my preference.
Drag the files into the Navigator view directly from the native file manager. You must create any needed Java packages first. This method is best for a few files in an existing Java package.
Use File -> Import... - select File System. Here you can then select exactly which files to import into the new project and in which Java package to put them. This is extremely handy if you want to import many files or there are multiple Java packages.
Copy the fires directly to the folder/directory in the workspace and then use File -> Refresh to refresh the Eclipse view of the native system. Remember to select the new project before the refresh.
The last one is what you did - minus the refresh...
While creating a project from a full folder may or may not work within the workspace, there's a condition outside of the workspace that prevents starting a new project with a full folder.
This is relevant if you use numerous folder locations for sources, for example an htdocs or www folder for web projects, and a different location for desktop Java applications.
The condition mentioned occurs when Eclipse is told to create a new project, and given a full folder outside the workspace. Eclipse will say the folder isn't empty, and prevent creating a new project within the given folder. I haven't found a way around this, and any solution requires extra steps.
My favorite solution is as follows
Rename the full folder with an appended "Original" or "Backup.
Create the Eclipse project with the name of the full folder before the folder was renamed.
Copy all the relabeled full folders contents into the new project folder.
Eclipse should make a new project, and update that project with the new folder contents as it scans for changes. The existing sources are now part of the new project.
Although you had to perform three extra steps, you now have a backup with the original sources available, and are also able to use a copy of them in an existing project. If storage space is a concern, simply move/cut the source rather than fully copy the original folder contents.
If you creating a new project based on an existing Maven structure :
Create the project using a general project wizard and give the project the same name as just created.
If you try to create the project as a Maven project via m2e will receive an error that project/pom already exists.
Create a new project..
Right Click on your project..
Select Build path --> Configure Build Path
Under source tab choose link source, your .java files containing folder..
I am suggesting this since none of the methods that you tried have worked ---FYI
Follow this instructions from standard eclipse docs.
From the main menu bar, select command link File > Import.... The Import wizard opens.
Select General > Existing Project into Workspace and click Next.
Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects.
Under Projects select the project or projects which you would like to import.
Click Finish to start the import.

Categories