How to use Java packages with Groovy - java

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.

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.

Is this code importing something from a folder?

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

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 classes from folder

I am new to android and i want to import some classes to my application wich are placed in MyClasses folder in same package.
After googling for a time I found a way to do this by usig Java build path and in this add class folder. I think this may solve my problem but no luck.
I don't know how to import classes from folders. Is it possible to import classes from folder? If it is possible then how to do this?
Any help will be appreciated.
I assume this is for Eclipse. You have the options for importing a Class Folder, which is not very useful in my opinion since it only loads folders in projects you have defined in your workspace. In this scenario I would just link to the project itself. But you can also import External Class Folders. If this is what you are trying to accomplish then make sure you are using the correct item in the Java Build Path dialog.
The problem that Dalvik does not understand java files. They should be compiled into the internal Dalvik format dex. And even after that I'm not sure that you can dynamically load files into your project.
Have the folder in your build path, then use:
Class.forName("package.className")
It will also call the static constructor for the classes loaded this way (this pattern is used with JDBC drivers).
If you want to change the folder at runtime, you might have to write your own class loader.
This might help more: http://www.techrepublic.com/article/get-the-most-out-of-javas-class-loaders/6080883
Not sure how this works with Android, though. I don't think you can implement your own class loader for Android due to security issues.

Change the package of a JAR from the default package

I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.

Categories