Is this code importing something from a folder? - java

This is part of a demo code, I am trying to learn this code
import com.jgrindall.android.connect4.lib.board.*;
What is this code doing? is a lib a folder? if it is a folder then where is it located?

It's importing all classes in the com.jgrindall.android.connect4.lib.board package. The source for this can vary - I assume it's in the com/jgrindall/android/connect4/lib/board source folders but sometimes it refers to code in an existing jar library used in the project.

Import means you want to be able to use the named classes without having to specify their full qualified name. In particular, since this import ends in *, you're saying you want to be able to use any class in the com.jgrindall.android.connect4.lib.board package by just giving the short classname -- Board rather than com.jgrindall.android.connect4.lib.board.Board
Where those classes are loaded from is a separate question, determined by your classpath and classloaders.

it is importing a PAKAGE. You can see the package as folder, and the ending wildchar means import all class in the packge.
If you are using plain source code, then package are folder, but they can also be packaged (no pun intended) in a jar. you can open a jar as a zip, and you will se a manifest file, and thepackage structure.
Also there ase some standard class in their own package in the visrtual machine, and they are all the standard library

Related

How to import .class file into a .java file in eclipse correctly?

I need to import a few class files into a java project in eclipse. I put the files in a folder and then add the folder to my project by right clicking the project, then select Properties > Java build path, then "Add External Class Folder".
Then I imported the class into the java file using an import statement:
import cla11.classname;
("cla11" is the folder name, "classname.class" is the name of the class file.)
However, the compiler doesn't allow the import (import cla11 cannot be resolved) and the classes contained in the class files are hence unusable in the project (... (class name) cannot be resolved to a type).
Note: I am aware that my question is almost the same as the one in this link: https://stackoverflow.com/questions/2477947/how-to-import-class-file-in-a-java-file#=
I used the method described in the answers, but the compiler does not allow it (as described).
(Since I am not yet allowed to comment, the only way I could think of is to ask the same question again.(Any other suggestions on what I could do to solve such problems would be welcome.))
Class' package name and your folder name must match. For example, if your class' full name is com.example.MyClass you need a directory structure like com/example/MyClass.class and import the root folder to the Eclipse.
Update
I do not know your actual needs. Like why do you even need to import an external .class file. So you may prefer to create a Jar file and add it as dependency. IMHO, This way it will be a lot easier to distribute your application.
You can create a jar file like this jar cf my-library.jar com/ after that, you can add Jar file as a dependency. For example if you are developing a web application you can simply drop your my-library.jar file to ${project-root}/WebContent/lib directory. Or if you developing a console application, you can simply add your my-library.jar file to your class-path.

jar libraries and separate .java files in Eclipse

I am programming in java using Eclipse, I am a novice and I'm stuck with the following problem: I have an Eclipse project that uses an external jar library. In this library there is a specific class that needs to be temporarily modified. I have the source code for this class. Is it possible to somehow include the source file for the code into the project, so that it will "override" the corresponding class in the jar file?
Thank you.
Basically, it's not possible to have two classes with the same signature (package + name) in the classpath but it's possible to import this class in your project in different package and use it instead of the original one.
Another way to solve this problem is to edit the .jar file by removing or changing the class that you need to be different.
However, note that changing an API is almost never a good idea.

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.

Java Imports to Jar file

My question is this is there a way to put all the Java Imports in a jar file, so when you make a Java Program no need to import the files individually????
EDIT:
I mean like
import java.awt.*;
import java.awt.event.*;
into a jar file where u don't need to type out the import java.awt.*; everytime
You can use netbeans to do that http://java.sun.com/developer/technicalArticles/java_warehouse/single_jar/
If you're thinking of something like a C/C++ global include file that allows a single import statement at the top of files -- then no.
However, your Java IDE should be capable of adding imports for you automatically.
You are mixing two things - Java packages and jar files.
A package is not much more than a name space for classes often used together. The import java.awt.* statement does not add the AWT classes to your program, it just lets you write Graphics instead of java.awt.Graphics and similar. There is nothing you can do to avoid importing them, if you don't want to spell them out (which is even more annoying).
To be able to use a class at runtime, this class has to be available to the virtual machine - independently whether you imported it or wrote its name fully. The standard classes like java.awt.* are usually already known to your VM (since they are part of the runtime environment). To use other classes (including your own ones), you need to include them to the class path (e.g. by the -cp argument to the java command, or by putting them in the current directory), or you need to load them by an own classloader. Here you can use jar files - the class files can either be in jar files mentioned in the class path, or be inside of directories mentioned in the class path. (The URLClassloader works the same way.)
But again, packaging classes in jars does not avoid importing them to be able to use them. One of them concerns the compiler's name resolution, the other concerns the runtime class loading.

How to use Java packages with Groovy

I'm at school and we are doing this java stuff. We use a special library named sum.kern: http://www.mg-werl.de/sum/bjw.zip
I want to use Groovy instead of Java. But I can't get groovy to use this library. If I add the jar in grape and try to import sum.kern and create a sum.kern.Bildschirm it says that it is not able to find it. How to do it?
I don't think it should be a problem, its pretty straight forward. The only requirement is to have the library in your classpath.
Are you able to import the package successfully?
It doesn't matter though, What IDE are you using, BTW?
You should have a "groovy-starter.conf" file that specifies the directories in which groovy looks for jar files. It should be in the "conf" directory within groovy.home (for example, on my machine it is "C:\groovy\Groovy-1.5.6\conf\groovy-starter.conf").
If the jar file containing the classes you want to import are within one of these directories, you should be able to import it no problem. Alternatively, you could add the directory that the jar is currently in to your groovy-starter.conf file.

Categories