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.
Related
I know how to add .java/class files to a certain project, or import them into a package. So, when you're using eclipse you can import java.awt.; and java.util.; and the but those are all packages inside the JRE. So my real question is: is there any way to import a package containing the files i want into the JRE so that, if i create a new project, I can simply import them whenever I need them? Thanks!
Yes. Generally, you have more then one choice. You can link the project(s) in eclipse, and then have project B with a reference to project A. Or, you can create a build artifact like a jar file and add it to the project B build/run path. Simply add a jar file to your project, right click and select Add to Build Path. Another alternative is to use a build tool that manages dependencies like gradle, sbt, maven, or apache ivy. Note that there are a wide variety of third-party opensource libraries you can add as dependencies using these (and other) tools.
You seem to be asking how to create a shared library, which in Java is a .jar file. The steps to do this in Eclipse are:
In the Package Explorer, you can optionally pre-select one or more Java elements to export. (These will be automatically selected in the Opens the JAR export wizard JAR Package Specification wizard page, described in Step 4.)
Either from the context menu or from the menu bar's File menu, select Export.
Expand the Java node and select JAR file. Click Next.
In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.
Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
In the Select the export destination field, either type or click Browse to select a location for the JAR file.
Select or clear the Compress the contents of the JAR file checkbox.
Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
You have two options:
Click Finish to create the JAR file immediately.
Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
Credit goes to the docs.
I am new to eclipse and I have a general question about referencing external Jar files.
I went to the buildpath -> configure build path and added my two jar files that I wanted to reference as parts of an external library but I can seem to make an instance object of the classes in that jar.
And also I tried just copy and pasting the jar files to the extension folder of the standard library too but this also doesn't work.
How can I use the instance objects of the classes included in these jar files?
Even if you build path to external JARs you have to import the required classes because they do not actually exist inside the same package you are working at.
if u want to add external jars into eclipse follow these steps
Rightclick on project - Select Configure Build Path... and the project properties window will appear showing your build path configurations.
Click Add Variable --Click Configure Variables
Click New -Type a name for the new variable.--Browse to the directory that contains the JAR for the path.
Click Ok to define the variable.--Click Ok to close the preferences dialog.
Select the variable from the list.--Click Extend..
Select the JAR(s) that you want to add to the classpath.--Select the JAR(s) that you want to add to the classpath.-Click Ok to close the new classpath variable dialog.
If you already added jar file into classpath and if it doesn't work I would try below on command prompt
java -classpath example.jar;
I have create a project under eclipse. Eclipse automatically creates a directory named 'src'.
The Source control system I am using imposes a directory structure which begins src/java/com/company_name/package_name/java_class_name.java .Other packages which are dependent i.e. use the java files in other eclipse projects refer to the classes with import com.company_name.package_name.java_class_name.
My question is in order for the above to work I need to create a 'java' directory under the autogenerated 'src' and link this to the directory where my files are starting 'com'. If I select "project-properties-Java Build Path-Add Folder-link source" then specify my directory structure 'src/java' it creates a java directory at the same level as my src directory. If I select add folder it presents me with existing folders only which don't include 'java'. I cannot see a way to resolve this.
Right click on the src folder and select New / Other... / Folder (not Source Folder). Type the name 'java' into the dialog then click OK.
But, that's not what you want. What you really want is to remove the source folder 'src' then add a new source folder 'src/java'. This is so that the package names are 'com.blah...' and not 'java.com.blah...'.
So, first delete the src folder.
right click on src
select Delete
confirm
Next, create a new source folder.
Right click on the project
select New / Source Folder
type in 'src/java' (no quotes)
click OK
Now you have a new source folder with the proper path, and the package names will all be as expected in Eclipse.
Modified alternative to Zagrev's answer:
You still need to delete the src directory from Eclipse project
Manually create the Java subdirectory.
Then use Build Path-> Link Source... and link it to the created directory.
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
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.