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.
Related
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.
I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all
I have started to learn Java along with Eclipse and the book "Thinking in Java" by Bruce Eckel. I tried to add his util package (from net.mindview.util) to a project but Eclipse complains the name cannot be resolved.
I added an external folder net. It contains other folders atunit, simple and util which containts uncompiled *.java files. In Java Build Path -> Libraries, I have pointed at the external class folder:
but Eclipse still cannot resolve the package name:
For me, everything seems OK and the package structure is correctly organised in folders:
Just in case, I also checked whether files correctly specify package name:
I run out of options. The Eclipse help just shows how to add external folders and does not say what to do if there are problems.
Hopefully someone can help with that. Thanks.
You've got one directory too far when adding the library to the build path. The library should be showing up as TIJ4Code, in the Java directory. (Or in other words, when you choose "Add class folder" you should be choosing TIJ4Code, not net.)
That way net becomes the first part of the package name - whereas Eclipse is currently treating net as the "root" directory of the library, so it thinks that Print "should" be in a package of mindview.util for example.
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. :)
I have a Mule project (created with Mule Studio, a flavor of Eclipse.) I would like to use the android.net MailTo class, which requires a few other files within the package, such as a ParseException and URI class. I could add the files into its own package in my project, however I've read I could import the package as a jar file. I can't find the jar file anywhere, and was wondering if there's a way to create a jar file out of only a portion of the package (without classes I don't need.)
Where can I find a copy of android.net as either source or JAR file? And if I get only the source, how can I compile into a JAR file?
Thanks,
Where can I find a copy of android.net as either source or JAR file?
There is no JAR file, strictly speaking, except as part of a firmware build.
The Android source code is available online, including the Java classes that are part of the android.net package.
However, getting this code to work on standard Java may be painful. I would recommend finding existing ordinary Java code.