I have just started Object Oriented Programming, so I have very basic knowledge in programming and using IDEs. I am currently using Eclipse. I have got a project to make a robot do some specific tasks and to them i need to inherit classes from the becker folder. If i use another IDE such as JCreator or DocJava or ReadyToProgramWithJava, the IDE inherits the becker folder when i save the .java files in the same folder as the becker folder.
i have already used the following code on the top of my program to import the classes
"import becker.robots.*;" but it keeps saying that it cannot be resolved to a type.
I have tried to use this link to solve the problem
(How do I include .class files in my project in Eclipse? (Java)) but i failed to make the code work. It is possible that i might have done something wrong so i was wondering if someone could be me a step by step method so i can make my code inherit the class files from the folder "becker".
Any help is appreciated.
Related
So from what I understand all classes need to be within a Java project in order to run them. However, when I just downloaded a Java class it automatically opened up in Eclipse outside of a project. Because of this I also cannot simply move it, as Refactor->Move gives the error "the resource is not on a build path of a Java project". So basically because it is not in a project, I cannot move it into a project? Is there a way to overcome this without just copying the class contents into a newly created class?
Edit: also I did not have this problem until a few days ago, when I moved the Eclipse application from a random folder in which it was previously installed, to the program files folder. If anyone could shed a light on why this might have triggered it, and why this predicament was not happening before this would also be appreciated.
I have scoured this website for the answer, but none of the solutions I have found worked for me. Please mark as duplicate and paste link if you know that post is the answer.
Anyway, what is happening is I am using Eclipse for my Java project. My professor emailed us classes that he coded and wants us to use for part of the project. I have not been able to figure out for the life of me how eclipse imports these .class files into the project. I have tried several solutions from other posts like linking the folder with the .class files, and none of that worked.
The reason I know it does not work is because I am supposed to be able to do "extends MessageState" etc. (MessageState is one of the .class files he sent us), and yet I get an error that Eclipse doesn't know what that is referencing (because it thinks it does not exist). The classes he sent are all super classes of my subclasses that I have to code myself in the project.
Let me know any ideas you have and I will try them out.
"EDIT"
The link provided was the first thing I tried, and it does not work. I still am getting an error in my class that extends one of the classes he provided to us. I need to use what is in his superclasses (.class, not .java) for my subclasses (.java) that I am currently writing. Here is what it looks like:
public class ServerBeginState extends MessageState
The error is on the "MessageState" which eclipse says: "MessageState cannot be resolved to a type"
This would mean that MessageState doesn't exist and needs to be created, but for some reason I am not linking the classes to this project correctly. That is what I am asking the question for.
So i just decompiled the .class files and made them into new .java files. That seemed to have worked, but if someone finds a way to use .class files for implementation in eclipse (because i know it works in Textpad 8) please post an answer.
Simply Create a new java project.
Then drag and drop the classes sent by your Professor in the folder named source code or "src" .
Eclipse will produce a pop up window with 2 options make sure you select "copy files"
if that does not work try manually copying the files in your src code location which will be found in your java project folder.
I am working with Paho project for Android. According to the instructions, while testing Android sample app, it is required to have both AndroidService.jar and JavaClient.jar included in the project. I import them in Android Studio and the app is working perfectly.
However, and that is my problem, I want to edit these precompiled java classes. But the problem is that, since they are imported as .jar files and that they are already compiled, they are uneditable in Android Studio.
I have the source code of the libraries I want to include into the project. Unfortunately, when I import them in the Android Studio, I get bunch of errors (which I do not usually get when I import .jar files).
The methods I tried up until now are to copy the source java classes into the project by creating new classes with same names and content. I also tried to import the whole source project as the module to my project. I get same errors. I also know about this great fork of the Paho project which lets you just to import whole project in Android Studio and it simply works. However, the AndroidService classes are still uneditable.
As I said, I have the needed source code. I can generate these .jar files without any problem from the source and then import them in the project. That method works. But to edit the java classes, then to compile them, then to import them in the Android Studio to test everything, and to do it every time when I change something, is a bit tiring. That is why I am asking for help.
Please, let me know if you know some approach which will let me to edit these two "libraries" while I am still in Android Studio, so I can test changes I make immediately.
P.S. I have been Googling this so much and I believe that there is no ready solution for these online. However, If you find one, I will be really happy if you point to it.
Fist of all I have no idea why you want to edit the Paho Android Service because it doesn't make to much sense. You are able to overwrite every class if necessary. Any way you would need to change the Android Service build from Maven to Gradle to include the source into you project.
To help you out I have just done that and pushed to the "withSouce" branch of my repository. https://github.com/sandro-k/org.eclipse.paho.android.service.sample/tree/withSource
If you now want to replace the compile org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2 with your sources as well you have to replace that with a local dependency as well.
Jar files are really just compressed (zipped) versions of .class files (and a few other essential documents). Class files are compiled .java files. The only way to edit these jar files would be to unzip the jar with a program such as 7zip or WinRAR, then decompile the class to get the source code, edit it, and recompile. Decompiling is a very difficult, time consuming process, and would require external software. Your best bet would be to continue the process you've been using.
Note: If you still wish to decompile, you might want to check out Cavaj, a free java decompiler. But beware, it may not return the exact source code used in the jar.
Sources: Experience
I am trying to learn java and have implemented a single, and simple, class in Intellij IDEA 14.1.3. I want to be able to use this class in other projects without copy and pasting the class source into each project src directory. Essentially, I want to create a package, or module (I'm not sure which, which is part of the problem) that I can simply import into any projects's src that I am working on--kind of like my own, one-class, library. While trying to figure out how to do this, I came across the two following blog posts--
http://blog.jetbrains.com/idea/2011/10/new-in-11-create-library-from-project-view/
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
--but when I tried to do what they instructed (creating artifacts in Project structure, etc.) I wasn't able to because my class doesn't have a main() method, as its not meant to. So I was wondering if I was going about this right--is there a way to do this, or am I stuck with copying and pasting?
I managed to get it--I had to go into Project Structure and set it to create a jar file containing my compiled .class file upon build. Then I could add the jar file as an external library in my other projects. Thanks guys for the replies.
You only need a main method if you want the package to be able to run as a standalone application.
So, to answer your question: no.
You don't need a main method to create a package, nor to import or use the package/library in a separate application.
I'm learning Java from Bruce Eckel's Thinking in Java. After reading about packages early through the book, I thought I had got it, but when I decided to use the supplementary code(in the form of a single ZIP), I'm not so sure anymore.
The ZIP file contains packages in the correct hierarchy. I've an extracted copy of the ZIP too. Let's call this folder TIJ4. Both the ZIP and the folder are located on my Desktop. I'm working with an example Shapes.java which has an import statement like this:
import polymorphism.shape.*;. I imported this folder in the Project Properties>Libraries>Add external class folder. The strange thing is that Eclipse does not show an error on the import line, yet it cannot resolve names to types. I could manually create a package, then a class from within Eclipse, but isn't there an easier way to do this?
I've gooogled about this but nobody seems to have the exact problem as me. Here's what I've got so far:
1)New Java Project created. Created a class file Shapes.java in the project:
2) Project Properties>Libraries>Add External Class Folder. Selected my folder TIJ4. Notice that the error on the import is now gone. Apparently Eclipse has found the polymorphism package.
3)But I still get XXX cannot be resolved to a type errors all over the place!
what do I do? I also tried Project>Build Path>Add external archives and selected the ZIP, however, ended up in the same place.
As the Zip file contains only Java sources, you will have to compile them. Simple way, right now, is extract them into the src directory in your eclipse project. That way they will be compiled and automatically included in the classpath.
The reason for not able to resolve the classes even the import error goes away, is the zip file contains the directories that satisfy the package structure but there are no compiled classes (the .class files) are found in them.