using other java files in my java project [closed] - java

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>

Related

The declared package "facts" does not match the expected package ""

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

In JAVA, how do I determine where an import is coming from?

Am not using any IDE and trying to fix some errors for a deployed war file in tomcat. I am trying to look for source of a package and Erroneous line seems to require parameters and has been imported from some package like
import com.somefirm.somepackage.someClass;
Following questions did not have answer my question:
In eclipse determine which jar file a class is from
How can I find files imported in a java class
I want to know is there any way I can find source of import manually. Is it even possible or not? How does a class look for packages to import?
Edit 1: Separated the links with a newline.
Edit 2: "am not using any IDE at the moment" was a bit late in the question. SO added Am not using IDE to first line.
Edit 3: Provided more clarity to the question, as to why I am needing it.
Edit 4: Added these edits. Thanks to #Jude-niroshan and #ErwinBolwidt
The point is: the exact location of a class is determined by your class path setup.
You define at some point which classes are available when compiling your application respectively which classes to ship with it.
So, when you are not using an ide - you have to search the "elements" in the class path that gets applied for your build. For example by looking into each jar file.
Given your comments: I think you have to step back. You seem to lack basic knowledge of Java. You have to understand how that WAR file is built. There should be some sort of build description; containing the dependencies and other contents of the WAR delivery. You have to analyse those. Beyond that: if these packages are from your team/company/... a simple file search might do the job. If those packages are "external", like open source libraries - then you might try to simple google for the class name; or turn to grepcode.com.
And the other thing you asked: a compiled class contains only fully qualified class names. There are no import statements in class files any more. So when a class "needs" another class, it asks the JVM to load that class (given the fully qualified name). And the JVM simply looks into the classpath, and loads the first class that matches the given name.

How to call java classes of a compiled jar file within android studio?

Hello StackOverflow folks,
I have a jar file of java classes. I added this jar file to my android studio project under folder /libs. Now, what I want to do is use those classes within the jar file in MainActiviy.java. I just do not know how.
Some details:
My jar file is named: zombi.jar.
The class within the jar file to call is named: COMBI.class
I tried the following:
In MainActivity.java, I wrote:
// declared class variable
private COMBI mCOMBI;
Then, in OnCreate method, I wrote:
mCOMBI = new COMBI();
//to start calling method COMBIStart to launch the command-line system
mCOMBI.COMBIStart();
I actually called the classes as I would in normal Java. I think Android uses special java code that looks like java, but I don't know how to use them.
I could not get the code to work.
Can you help me?
I'm going to assume you've already written your import statement for the jar class. If you already put the jar file in the /lib folder, Android Studio should update your build.gradle file. Check to see if you have a link to the jar file in your dependencies{...}
If not, you can add it manually.
Just like in normal java you have to add an import to the correct package at the top of your file for everything that's not in the same package the file is in.
It should look something like this:
import android.view.View;
but instead of android.view.view it would be a reference to the class in the jar you're trying to add

Algorithm of package importing in Java

I'm really new to Java and have to constantly formulate and solve questions. But the only thing I'm not able to do at all on my own is to import packages to Java. I've looked through answers to many similar questions, but they either don't work in my case or very unclear.
The problem is very general: I have package in the directory X, containing jar file X-4.9.jar, source files in X/src subdirectory and usual other directories. I set up CLASSPATH to directory Y by using command prompt, such that Y contains X. I need to figure out how to import it in my public class. As I understand I will need to write:
1) import Name1.Name2...NameK.*; Question: how to find out Name1, Name2..?
2) Do I need to do something else?
I use NetBeans IDE and don't have the menu option to add library or jar to the project an my class is not an application. I also use Maven for the first build of each class I write (basically write a library), but then use NetBeans as it's faster.
I need the simplest guaranteed to work way to accomplish the goal, even if it's ugly. The link to clear explanation on how the algorithm can be calculated will be also appreciated.
EDIT: Suppose I need to import CSSBox package. Do I write in my import statement
cssbox.* or net.sf.cssbox.? Why not some.hard.to.find.package.? By Name1, Name2 I indeed mean names, not the packages/classes it refers to.
Read the answers to this question: Reflection
This will tell you how close you can get to finding out Name1 and Name2, but you simply can't do it. Those import statements are thrown away before the class is compiled to an object file.
Read this question for a detailed explanation: Finding imports with reflection

How can I include a file from the directory above in Java?

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.

Categories

Resources