Algorithm of package importing in Java - 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

Related

How to resolve "org.name1.name2.name3;" imports?

I am always confused by the Java imports like "import com.smth.smth" or "import org.name1.name2.smth", which are errors in my Java code.
How one has to resolve them, possibly with Eclipse?
What are the names of such imports? I tried to google it, but the answers I find always say to download some .jar and add it to the build path through "Add external..."
I do not understand these imports looking like web-addresses. How do they work? Explane me please, or give a reference.
Thank you.
Picture 1:
Picture 2:
In Project > Properties: Java Build Path in the tab Libraries a library/dependency (for instance a JAR or class folder) that contains the types (classes, interfaces, enums or annotations) org.myrobotlab.service.interfaces.DeviceControl etc. have to be added.
If you have a module-info.java file, in addition requires ... statements for the modules containing the types have to be added.
import statements are a fundamental thing in Java, so it would be best if you would learn the basics of Java first.
when importing from a .jar you are just accessing the classes within as if they were with all of your other classes. you have to download the jar and add it too your build path to use anything in it.
organizations or companies usually title their .jar file's packages with org/com (if they are company or organization), then sub package (what the package does) , and then the class name .

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.

using other java files in my java project [closed]

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>

JPype Headaches

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()

Import an Ant classpatch from another file

I'm exploring the awesomeness of Ant 1.8.1's import ability. Here's my situation: I have a top-level Ant file (project.xml) that turns around and calls ant on another Ant file (say, neato_project.xml) which actually does the build, or clean or whatever.
I have 12 different project files that this top-level (project.xml) file can call, so I want to put a common classpath entry into the project.xml file that I can pass to the others to use as their individual classpaths.
How do I do that? I've been trying to play with import task, but I haven't gotten that figured out. I'm open to another approach if there's a better way to approach this problem in Ant.
Import wasn't introduced in 1.8; it was enhanced in 1.8. This is good because it means people like me have a couple years experience with import.
What I do:
constants.xml - the common strings and classpaths my build uses
build.xml - the main file imports #1 and #3
helper-project-1.xml - it has a clearer name, but that's hardly the poing
helper-project-2.xml, etc
I use this approach because I want build.xml to pass the constants. I only keep them in a separate file for readability.

Categories

Resources