I am using Eclipse IDE for Enterprise java and web developers. I downloaded some zip file called facts(1) and followed every step in setting up this facts project. When I try to run it on server it showing " ... doesn't match the expected package. I tried by adding import package facts; and also without that! I have also looked similar answer and didn't able to fix that either. FYI i have jdk (16.0.2) currently.
[enter image descripenter image description heretion here]2
enter image description here
As you are using default package for your facts classes like Fact.java, FactList.java, you do not need to import it.
However, I think your fact files Fact.java, FactList.java, etc are also failing compilation as the package declaration on those files are not same as the folder structure.
So please try these:
Make sure that there is no statement
package xxx.yyy.zzz;
in your fact classes Fact.java, FactList.java, etc and they are compiling successfully i.e. no errors in those files.
Remove the import statement in FactServlet.java as it is not required.
But.. according to Java standards using default package is not advisable. Create proper directory structure for your files.
you classes Fact.java, FactList.java, FactSearchModel.java and few more are in default package
create a package and move these classes to package, then import that package wherever required
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
first of all, I have to say that this is my first attempt to do something with eclipse and java. Now, I've just open a new java project in eclipse called alignmentComparision. In addition to this, I have a java code, which I need to use in my newly created alignmentComparision project. To explain my problem clearly, I can give a similar c++ example. Assume that I have a file superposition.cpp and superposition.h and I created a cpp file whose name is alignmentComparision.cpp. By getting superposition.h, superposition.cpp and alignmentComparision.cpp at the same folder and adding #include superposition.h line to my alignmentComparision.cpp , I can call the functions inside superposition file. How could I do similar thing in the eclipse ?
Use import path.to.my.Class;. If you struggle with basic things such as this, I would suggest getting to know Javas syntax by doing a Tutorial.
I assume you see following structure in eclipse package explorer like below:
alignmentComparision
src
default package
A.java
B.java
if you want to call method say static in A from B, you could do something like:
A.abc();
If you want to create new instance of A and call method from it, you could do the following:
A a = new A();//similar to C++ new operator to create instance
a.method();
Similar to include in C++, you have import in java. So if you have your java source in multiple directories (we call it package in java), you could import those. Instead of specifying exact name as in C++, you could use wildcard in Java something like:
import com.mycompany.mypackage.*;
For More details checkout oracle's doc.
In Java you create your source files in packages.
A package is actually a directory path that is mentioned on top of your source code.
For instance your source Superposition.java file could be in package com.mycompany.myapp
As consequences this source file is in directory com/mycompany/myapp starting from your root source directory.
The first line of code of this source must be package com.mycompany.myapp;.
In this source file if you need to use another class that is defined in another source file in the same package you do not have to do anything special. Just use it.
But if you need to use another class from a source file that is in another package than you must import that class just below the package statement and before the class definition.
Example:
package com.mycompay.myapp.service;
import com.mycompany.myapp.model.MyModelClass;
class MyClass {
MyModelClass model = new MyModelClass();
// code here
}
You may also import all the classes from a package. for instance if you want to use several classes from package com.mycompany.myapp.model you may add an import statement for each or use a wildcard: import com.mycompany.myapp.*; With this you can use all classes from that package in your class MyClass. Usually IDE automatically add import statements and you do not have to bother with them if the classes you need are in the classpath. It is the case if you are using classes from the same project.
To automatically add import statement with Eclipse press Ctrl+Shift+O and with netbeans Ctrl+Shift+I
This has nothing to do with eclipse and everything to do with Java. You need to make sure the different files are in the same package or are imported, similar to what you need to do in C++ (but using different keywords).
In eclipse right click on the folder that your project is in and select "Build Path" then select "Add External Archive..."
Then you need to do the import the class into your program.
Without doing the
import <pathtojar>/<jarfile>
this will allow you to do this
import <jarfile>
I've found several instructions on how to import user-built .class and .jar files to JPype, but I seem to be having a lot of trouble getting anything working at all.
What works: I can import standard java stuff and print HELLO WORLD and such.
Some of what I've tried:
I've tried adding -Djava.class.path with the path to a jar containing the relevant class files, to a directory structure containing (several folders down) the relevant .class files, as well as '-Djava.ext.dirs'. I've recompiled and re-installed with a different JVM location. The class I am attempting to instantiate is Outer, public, and has a public constructor.
I'm using Python 2.6.1 on OSX 10.6.
My current test file:
from jpype import *
startJVM(getDefaultJVMPath(), '-Djava.class.path=/Users/gestalt/Documents/msmexplorer_git/msmexplorer/MSMExplorer/build/classes')
java.lang.System.out.println("hello world")
msmexplorer = JPackage('org.joofee.meh.msmexplorer')
T = msmexplorer.MSMExplorer()
shutdownJVM()
If I use JClass I always get ClassNotFound exceptions from JPype; if I use JPackage I get Package not callable errors. Basically, JPype can't find my stuff.
Thanks so much!
EDIT (possibly helpful debugging stuff...):
Is there a straightforward way to print which third party java classes are available/imported?
Package not callable errors are referenced in this link) it would seem you need to make sure the java class file is accessible from the working directory. I am not sure how the jvm classpath comes into play, I would have thought how you did it would work.
You could also try loading the org package and then getting to the other packages through that one as the link I shared shows:
msmexplorer = JPackage('org').joofee.meh.msmexplorer
T = msmexplorer.MSMExplorer()
Can anybody help me out in this?
what I have is a abc.jar file with me. It contains ABC.class file inside it. I added the jar file to netbeans project Libraries. But I am getting an error when I write
ABC a=new ABC();
Error :
"can not find symbol class ABC"
any help?
Edited :
also I am able to see the structure of ABC class when I click on the ABC.class file inside the library.
Remember to import it with an import statement. You should probably read the documentation that follows the jar file. Hopefully there's an example of usage - mostly there is.
The answer :
I found out that if the jar has only default package you won't be able to import classes inside it. You need to have packages other than default inside the jar to import it. You can only import non default packages.
the thing with default packages works fins in eclipse, but not in netbeans.
I would like to know What are the difference between folder-structure and package used in Eclipse IDE for Java EE development.
When do we use which one and why?.
Whats should be the practice
create a folder structure like src/com/utils and then create a class inside it
create a package like src.com.util and then create a class inside it
which option would be better and easy to deploy if i have to write a ant script later for deployment ?
if i go for the folder-structure will the deployment is as easy as copying files from development to deployment target ?
If you configured stuffs correctly. Adding a folder inside src, is same as adding a package from File > New Package.
So, it's up to you, whatever feels comfortable to you -- add a folder or create a package. Also, when you put stuffs under src the package name starts from subfolder. So, src/com/naishe/test will be package com.naishe.test.
Basically there is no difference, both are the same.
In both the cases, the folder structure will be src/com/utils.
and in both the cases, you will need to mention
package com.utils;
as first line in the class
Since it doesn't have any difference practically, it won't make any difference to ant script.
"Packaging helps us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways."
check out http://www.jarticles.com/package/package_eng.html for more information on packages
create a package like 'src.com.util'
That sounds like a mistake. The package name should be 'com.util', and 'src' is the name of the source folder.
Other than that, I fail to see what the difference is between your two choices. The result is the same, right? Just different steps in the GUI to arrive at it. The wizard to create a new package in Eclipse is just a wrapper around creating the appropriate folder hierarchy within a source folder.
You don't need to create empty packages at all, you can directly create classes (the package will be created automatically if it does not already exist).
A package is automatically "source folder" where folder is just a normal folder.
When you compile an Eclipse project, all files in source folders are compiled but not in regular folders (unless those regular folders a)
folder structure or to be specific source folder in eclipse is meant just for eclipse but package is universal irrespective of any editor..
I'm not sure how to import a file from a directory above. That is, I have a setup like so
directory: MyProject
Main.java
directory: Other
Other.java
Basically, Main.java is in "MyProject" and Other.java is in a folder inside the project's root folder. I can easily do
import Other.*;
to get those files available in Main, but how do I get Main.java to be visible to Other.java?
import ../Main.java
Obviously this doesn't work, but that's the general functionality I'm looking for. Any suggestions? I would prefer not having to use absolute paths. Thanks!
Edit: I meant import not include. Sorry. Been using C++ too much.
Java does not include files. You can however directly use classes using the simple name by using import statements.
Basically you need a file per (top level) class you define. This allows IDE's to rename compilation units, and do other refactorings. Besides that, it lets you easily add code at the right spot.
Java does use packages to create namespaces. Packages themselves are completely separate namespaces. Although the namespace seems to be a tree structure, in Java each package is actually not related to any other package. Hence you cannot use it as a folder structure, using .. is not allowed. This may change once "super packages" are introduced.
The Java import statement looks a lot like #include, but the name change is deliberate: instead of grabbing the file to make the definitions in that file known, it is simply a statement to make it easier to refer to classes and interfaces. It has no other effect than having a shorter name to a class (or, for import static, constants and other static members).
Most of the time the top level classes are represented using a folder structure that reflects the package name. This makes it easy for IDE's and developers to find the file representing the class. It also makes it easy to put in version control. It is however not part of the Java specification itself; the location of Java source and classes is not defined. Earlier IBM IDE's actually stored Java source and classes in a database for instance; they did not use files at all. Newer IDE's such as Eclipse may use different source folders, e.g. one for Unit test files and one for the library itself.
So finally, the only way to include packages is by specifying the full package name, then a dot and then the class to import, or the * wildcard to import all classes of that package.
import java.util.Vector;
import java.util.*;
Most IDE's will create these import statements for you, possibly after you have chosen the right class to import (in case there are classes with the same name in different packages).
More information can be found in the Java Language Specification (Java 7 version).
In your case you have defined a Main class in the root or default package which is strongly discouraged. You can directly refer to Main without any import statement. The Other class is in the identically named Other package (using uppercase in package names is strongly discouraged as well). You can refer to it by using import Other.Other.
include ???
Java doesn't have file source inclusion support, it rather use a naming conversions, so you should import the namespace (package) that you need in your source file.
You should define a package for your main class and then import it in the Other class .
the Main.java is in the default package, this is impossible to import from other (named) packages
put it in a package and import as normal
directory: MyProject
directory: base
Main.java
directory: other
Other.java
(also package names are lowercase normally)
if you have file outside of your project it means this file:
wouldn't be compiled by project
wouldn't get into jar
can't be used in runtime
so you really shouldn't include it.
Either move it into project, or include dependent project which contains that file.
Java is not like C++. You include by package name. So if toplevel file is in project AAA in folder src/aaa then you should include that project as dependent jar and refer to file as import aaa.Main
I think import Main; should just work.
You should read up java concepts package and classpath. Please look at the documentation here. The options that will work for you are sourcepath and classpath.