Importing .java and .class file gone wrong in eclipse - java

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.

Related

How to edit .java files imported in Eclipse from a .jar file

I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.

how do i compile and run simple java files on eclipse from a src folder?

I am in an intro cs course learning Java and the prof gives us unfinished files to complete for our projects. The last projects have been simple and Ive been able to just open eclipse, start a new project, and create a new class with a the same name and just copy and paste the plain text from the files he gives us. However this time that approach isnt working. Basically I have the folder called "src" and i need to know exactly how I can open it in eclipse and start working on the project and be able to run it. Please help me.
basically i downloaded "Project3" from my prof's website. I unzipped it and it conatined project3.pdf - the project outline, and the folder named "src"
inside the src folder are Project3.java and sample3.java that are both used together in the project. All i need to know how to do is to be able to open them in eclipse and start working on it. If i copy and paste the plain text fro the java files to a new project in eclipse i get all kinds of errors.
First of all, you can just copy-paste the files into eclipse.
Second, you can use File -> Import -> General -> File System to import files from the file system into eclipse after creating a new Java project.

Issues getting files from my friend to work on my eclipse?

My friend sent me a project from his eclipse workspace for me to take a look at, however when I tried to put the folder in my workspace it did nothing. I opened eclipse and nothing was there. So i tried to make my own project and just put the .java and .class files in their respected folders, but it still didn't work. Is there some special way I have to transfer them? I should also mention he has a windows computer and I have a mac if that makes any difference for file types like that. Thanks for the help!
If you can just take the .java files and paste them into your 'src' folder in a new eclipse project, you should have no issues. Just make sure you refresh your project in eclipse(right-click on project folder and click refresh, or click on it and hit f5).
This is what i normally do.
Suppose your eclipse workspace is D:\EclipseWorkspace and your project name is Project1.
Then i would copy Project1 in eclipse workspace on disk to have D:\EclipseWorkspace\Project1.
Then create a new java project in eclipse and when prompted for name provide name as Project1 (exactly same) and say finish. It will import the project automatically. Everything should be up :)

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 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!

Categories

Resources