import package statement not working - java

I am trying to import a package into another project. Just to be clear, I am using NetBeans and all my projects are located in the same folder.
So, I have a package called createobjectdemo, and I am trying to import it into another project called valueOfDemo.
The statement that I am using is:
import createobjectdemo.*;
This line invokes the error: package createobjectdemo does not exit.
But... it does exist! It is located in the same folder hosting all my Java projects. I have it open right now in the NetBeans editor window.
Why can't I access the package? Do I have to provide a specific path? Sorry, this has got to be really simple, but I can't figure it out.
Thanks a lot.

You need to make .jar file of java files which you want to use in another project and add your project in menu Libraries to another project.

Not knowing exactly how your projects are set up makes it difficult to answer your question. I imagine your setup is something like the following:
projects/
project_1/
src/ // Or maybe you have src/ and classes/ in the same directory
createobjectdemo/
CreateObject.java
classes/
createobjectdemo/
CreateObject.class
project_2/
src
valueOfDemo/
ValueOf.java
classes/
valueOfDemo/
ValueOf.class
So, the package exists but it's not in your class path. You can tell NetBeans (I have no experience with it, but I'm sure you can) that you want project1's classes directory added to your build path, or possibly just add the first project to the build path, and NetBeans can figure out where to find the compiled classes.
Remember, each file in a package must declare that's it's in that package; otherwise, it just happens to be in that directory but (if the directory is on the classpath), it's in the "default" package, and can't be imported by package name.

Related

Eclipse cannot resolve package name

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.

Eclipse - How To Remove Packages

I just started taking this Distributed Systems class, and my teacher says the following on our class website:
Please do NOT use packages! If your configuration of Eclipse uses them
by default, please remove them before writing code!
What the heck is she talking about? I thought eclipse NEEDS to create packages for your project source files to stay organized. How do I remove them?
Packages are not mandatory. If you create a new project in Eclipse, you can directly add your classes to src folder. These files will be under /workspace/project_name/src/ folder. If you add a package, your class files will be placed under /workspace/project_name/src/package_name folder. Your instructor will need to know package names to be able to compile your files and she is probably using a script or something to automatically compile them and that script assumes your files are placed under src folder.
Just remove the first line of your code if exist that says:
package <package-name>;
And if you are creating a new class, then don't give any package name.

adding class files to java package in eclipse

I have three .class files that I'm supposed to black-box test. They are under a package named one.two.three. I'm having difficulty accessing them. I started a new java project in eclipse and created a package with the same name. I then proceeded to add the three .class files to bin/one/two/three. If I try using the classes Eclipse can't find them and I get compiler errors. I'm using the appropriate package header. Any help is greatly appreciated, thanks.
assuming bin/ is your output folder in the Eclipse project settings. Simply dropping the class files under bin/one/two/three will not work as eclipse will either delete them (on clean build) or just ignore these extra artifacts. Do as #Ray Tayek says- keep them in a different directory and edit project classpath and add this as class folder. This should work.

Unable to use existing Java packages in Eclipse?

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.

Java -- how do I access (import) the class files in a folder that is in the same directory as the src folder?

In eclipse or netbeans or emacs pico or microsoft word or notepad or or or whatever. Thanks.
In eclipse, I'm trying to share a package in one project w/ another project. I do the whole buildpath->link source -> add source ... and it creates a linked directory but not as package within the src/ dir, instead as a separate dir sibling to the src/. so I'm left stranded, wondering, waiting for a solution.
If you mean classes without a package, you can only refer from them outward, you can't refer from a class that is in a package to a class with no package declaration (A very good thing).
Even if you wanted to reference a class in the default package it would be in your "src" folder, not in the same directory as your src folder, so perhaps you mean something different?
Generally your "src" directory would contain a "com" directory--the top of the package tree. So if you had a class in the default package you would place it at the same level as "com", but as I said you can't refer to it from other code--you can only execute the unpackaged class from the command line.
If you are saying that you want all your classes in the same directory or something--then I recommend packaging them all together as a .jar file.
Normally you would add the project (with classes you want to share) to the Build Path (right-click>Build Path>Configure Build Path) of the consuming project. That allows you to compile and run within eclipse.
If you want to create a link from one project to the place where the other project code lives, you have to create your linked folder in such a way that 1) it's already in the /src/ folder or 2) can be turned into a source folder and can see the full package structure necessary to compile your classes. But that is not how most java projects consume the output of another java project.

Categories

Resources