How to find import associated with a class? - java

Within Eclipse if a class is not found and is available on classpath Eclipse can auto import the required package. What is the mechanism that allows this ?
I doubt there some repository of class files and their associated packages that Eclipse searches...
The reason I tagged this java and scala is I assume the mechanism is same for both languages.

It's as simple as searching through the whole classpath to see if it can match the class name. Then if it can, just adding the path it took to get there. There's nothing mysterious about this

The Eclipse feature simply searches the entire collection of folders in the classpath until it finds one or more matches, then it gives you those matches as options. In pseudocode:
Find Foo.class:
For Each FOLDER in CLASSPATH
For Each PATH...Foo.class Found There
import PATH...Foo.class
You can count JAR files as folders. To see their content, try this on the command-line:
jar tvf MyJar.jar
The class path includes jars containing all JRE classes.

Related

import java package into a jython project in Eclipse or PyCharm

Using Eclipse + PyDev + jython. Need to import a java package to use a Java class inside a Python program (using Max OSX).
For import, I mean statement in Python like from com.a.b.c. Wondering where should I put the Java jar file which contains com.a.b.c? Thanks.
BTW, if any PyCharm + jython based solution, it will be also great. :)
This question is not duplicate from the other one, the other one's title is bit mis-leading, and that one is about how to install jython.
The import semantics do not differ that much from CPythons from what I see in the Jython Docs.
First a search is made for the .jar file in the current directory; if it is not found there, it is looked up in the directory containing the core Jython libraries. The classpath which corresponds to Javas CLASSPATH is then searched along with the site-packages directory containing external libaries. I am not yet sure what __pyclasspath__ is.
So if a package is not found in those directories an import error is raised. You have two options:
Either add the .jar in one of the directories (typically you should never add it to the directory containing the core libs.
Add the .jar to your CLASSPATH.
Add the path to your .jar in sys.path.
For the first case, either move it to the current direcory or in site-packages.
For the second case see here on how to add a .jar to your CLASSPATH.
For the third simply call sys.path.append("path_to_jar") to include the directory containing your .jar file to sys.path.

How do I know the path for Java import?

I'm trying to learn Java ( I know php ) but I don't know what path the included classes have. For example, I create a new directory in Eclipse (in the package) and drag there a class from other project. When I try to import it, it cannot find the class. Even if I don't have any dirs and the class is directly in the package, using import package.classname doesn't work...
I must be missing something but googling doesn't show me any replies.
How do I get the class path? Is it somewhere in the properties?
Java has the concept of a classpath: a path where all classes should be found.
You can get the existing classpath with this code:
System.getProperty("java.class.path")
If you run java from the command line then you have to set your own classpath with your classes.
From the classpath, use the package to find the class. For example, if the classpath is ".", which is the current folder, and you have a class called A, which is in the package com.yourcompany, then you will find the class under ./com/yourcompany/A.class
On the example you gave, go to the terminal and look for the "bin" folder and you will see all classes. However, if you want to add a new class from another project to your project, then there are simpler ways. You can simply open your build path in Eclipse and add the class from the other project onto your project.
Another way is to create a jar from the other project and add the jar to your project.
In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.

Java NoSuchMethodError same class name

I have two .class file with the same name and same package in two different .jar file
First jar:
Second jar:
When i run the program from eclipse i haven't problem, eclipse use the first .class file (i must use the first . class, i don't need the second .class, i want exclude it).
When i export runnable .jar i saw that is executed the second .class file and then i have
NoSuchMethodError exception, because the second .class is different from the first.
How can i use always the first .class and exclude the second?
I don't need the second .class, but i need other class from his library.
Java loads classes from classpath that is defined dynamically when you are running application in eclipse and is controlled by property Class-Path in file MANIFEST.MF located under META-INF in you jar file.
So, first open jar file using any ZIP tool and take a look on manifest. Try to change the order of jar files into manifest and run again. I hope this will help.
BUT this is extremely bad that your alive-matchmarker.jar contains file that it should not contain. I do not know what library is it but is there a chance that they have other distribution that does not contain their own dependencies? Or probably try to find other version of this library. The worse thing that can be is if you have different versions of the same class in your classpath: the behavior of your application can be buggy and unpredictable as a result of this duplication because you can never know what version of class is used now.
Do not import the whole package like
import org.mindswap.*;
You can import specific class you want from any specific package like
import org.mindswap.wsdl.WSDLTranslator;

Import user defined package in java from outside of the subfolder

I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??
You'll need to have the location of the imported classes on your classpath when compiling your classes and running your code.
From your description and comment, it sounds like you have a package named world with a class named Balance, with Balance.class in c:\world. This should work from d:\java:
javac -cp %CLASSPATH%;c:\ SomeClass.java
Replace SomeClass.java with the name (one or more) of the classes that you're trying to compile in d:\java.
You'll also need to have c:\ in the classpath when you run your code.
If you are seeing what I think you are seeing (no c:\world in the specific error message), then it's easy. You need to add c:\world to your CLASSPATH setting.
After that, a simple import Balance (or whatever) should suffice.
Otherwise, a MWE (Minimal Working Example) illustrating the issue, and the exact error message you are getting, would be helpful.
You have 2 ways to solve this problem:
Make the packages available in a single project, this means, both packages would be in the same source directory.
Make a jar that contains your world package. Copy this jar to your lib folder in your project and add it to the classpath, now the world.Balance class can be reached in your current project.

In eclipse, is it possible to change what the default package is without changing the source folder?

I'm working with some people on a java project. Problem is, I'm the only one using eclipse. The source files are located in svn in trunk/src/*.java. However, if I import that as a project directory, the default package is "" instead of what the actual project package name is.
Is there a way to change that without changing the source location and the package name?
Thanks!
If you mean that you want code in package foo.bar without having a matching directory folder of foo/bar under some source root - no, I don't think Eclipse supports that. While the convention of source locations having to match package structure isn't enforced by the language specification, it's mentioned there and so widely respected that I think it would be a bad idea to do anything else.
Eclipse requires a directory structure that matches the package structure. There is no option to have some package prefix that isn't reflected in directories.
IntelliJ can work with this, and it's what most people expect to see most of the time anway.
I think you are checking out the incorrect root folder.
If you are trying to work with a collection of source files located under trunk/src/ you may be don't need to check out this folder, because you will loose your reference to the main package (for example foo.bar) because it will be the base package.
You may need to check out the trunk/ folder, because Eclipse expects to find the source files under the default /src folder. Once you have your main root folder (with a lot of files like .project, .classpath inside), it is likely possible that Eclipse will recognize your folder structure and configuration and your project will compile without problems.

Categories