I have a Java class that i would like to import into my Jython script. Unfortunately, eclipse won't let me create a Java class inside my Jython project.In the window, where you create and name your Java class, I get a message at the top (alongside a red cross) saying, "Source folder is not a Java project" when I type the name of the would be class. How do I rectify this? I need the Java Class to call C code using the JNI (declaring the native method,loading and then calling it). Thank You !!!!!!!
What you can do is to create second module which would be java project. Anyhow, logically it should be that way. Please check out other similar question - PyDev: Jython modules & Java classes in the same project.
Other links that might help - http://pydev.org/manual_101_project_conf2.html
So what nefo_x suggested is correct. You need to create a new Java project that will contain your Java class. Then import the Java package as you would a python module. But there are a few things to watch out for in eclipse to make it work. I list the whole process below:
Your Java class (or classes) should not be in the default package. You need to create a new package and make/put your java class files there.
Export the package as a jar file to some place on your computer.
Add the jar file (located at some place on your computer) to your python path.
Import the package by writing "import PackageName".
The problem for me was that I had my java class in the default package. That does not work due to some naming issues. Anyhow, hope that this helps.
Related
I have just recently started using Eclipse and am running into problems trying to install external libraries. Following online tutorials, I add the .jar file to the classpath and I see it in the referenced libraries folder. Despite this, when trying to import, I get the error:
The package org.apache.commons is not accessible
For reference, I am trying to install the apache math commons library.
Your code probably has two issues.
First, the import statement is wrong since in Java you cannot add a package itself, but all classes of a package as follows (note .*; at the end):
import org.apache.commons.math4.linear.*;
or a specific class, e.g.
import org.apache.commons.math4.linear.FieldMatrix;
Second, you use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9 and you have Java 12.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)
I am trying to make some changes to a legacy code of a plugin which was written using Java version 4. I am trying to extend a class from an imported package.
import org.eclipse.wst.xml.core.internal.document.XMLModelContext;
public class XMLModelContextForPma extends XMLModelContext
{
}
I'm quite new to plugin development. I couldn't figure out why the compiler shows
The type org.eclipse.wst.xml.core.internal.document.XMLModelContext is not visible error. Also, most of the codes in classes of this particular package are using .internal. packages which are giving Discouraged access warnings. I'm googled here and there and found it's because of non-standard/API classes.
But this is quite strange. I have the jar files in the build path but not sure what is wrong here.
I'm developing in Eclipse Juno, Mac OS X, Java 6
It looks like the class XMLModelContext is private or protected and in a different package.
If a class is declared as protected, you can only use it in other classes within the same package or any of it's sub packages.
Add that jar(org.eclipse.wst.xml.core.internal.document.XMLModelContext containing jar) to your project file path.
I have imported the algs4.jar file as a referenced library as seen above. All seems to be fine but I can't seem to use or access any of the classes in the library.
Is there an import statement I'm missing?
Image reference:
Note how I'm trying to use BinarySearch from the referenced library but it is not offering me an option to use or import it.
I am taking an Algorithms class via Coursera that utilizes the algs4.jar reference library.
The Problem:
The issue you are having is that the reference library is located in the default package and the source in which you are accessing the reference library is not.
The Solution
All classes in your project need to be in the default package, otherwise the reference libraries (which are located in the default package) will not be recognized. Your project classes need to be in the src directory and you should not declare a package at the beginning of your .java files. Additionally you also need to make sure you've added the reference libraries to your build path.
I am not sure, I did not try this. Some interpretations though.
Is this a standard library or someone created it by himself and gave you?
The problem is class BinarySearch is in default package. You will not be able to import it. If this library is created by you, or by any of your friend, you need to ask your friend to move all classes from default package to a good namespace and then re-create the jar.
If class is accessible, ctr + shift + o should import the class.
Note that the classes in the jar files are all contained in the default package.
If your classes are then contained in a package then they will not be directly accessible.
I submit this answer because I am taking the algorithms course to which these jar files belong and this was the specific cause of my program not working.
If you want to import the classes, you need to download the packaged version of the library. See the Q&A section at the bottom of this webpage.
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?
A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use the packaged version stdlib-package.jar.
Firstly, BinarySearch can not be created (private Constructor). Use the static Method BinarySearch.rank(int key, int[] array)
Secondly you can access BinarySearch only from default package
Thirdly what about Arrays.binarySearch(array, key) ?
Just delete module_info.java file and the referenced libraries are accessible instantly
I'm using PyDev 2.5 with Eclipse Indigo and Jython 2.5.3b1 . I have a JAR file that contains certain classes which I'm importing to a PyDev (Jython ) project. They seem to work seamlessly except for Auto completion. The member functions of Java Classes do not auto-complete e.g. pressing the dot '.' operator does not bring up the list for class member functions. The jar file is added to the PyDev-PYTHONPATH external libraries of the PyDev project.
Screenshot of PYTHONPATH external libs
Auto completion does not work for the code below, but it compiles and runs perfectly fine.
from my.testpackage import MyClass
myVar = MyClass("Monkey")
print myVar.getName()
Typing "myVar." does not auto complete
Worth noting that auto completion works if I imported a non custom jar
e.g.
from java.lang import Math
print Math.max(3,5)
Typing "Math." will auto complete
I'm not sure if this functionality even supported in the current version of PyDev. Does anyone actually have this working in their PyDev and Eclipse setup?
Any suggestion would be appreciated.
Thank you,
DM
It may be some issue in your PYTHONPATH configuration. Have you read: http://pydev.org/manual_101_project_conf2.html (most specifically the end of the page: "Project reference for Jython users").
If that doesn't help you, can you explain how are you referencing things? (a screenshot with the config would be nice)
i need an example in Developer defined package in java i cant find any example about it
as you know there are tow kinds of packages in java as the following definistion
Java Packages: The Java language provides the concept of a package as a way to group a number of related classes.
Types
– Standard Java Packages.
– Developer defined package.
brgd
A "developer defined package" is not some special kind of package. What is meant in the text that you quote is that a "developer defined package" is simply a package other than the packages in the standard Java API (packages named java.something, javax.something etc.).
Any package that you create yourself is a "developer defined package". See this tutorial page to learn about using and creating packages:
The Java Tutorials - Creating and Using Packages
Go through the java tutorials.
http://docs.oracle.com/javase/tutorial/java/package/packages.html (and following).
It is strange. 99.999% of code is created in user defined packages. Package is like directory. If you have source directory and want to create package com.mycompany.myprogram just create directories com/mycompany/myprogram under your source directory and then create classes there. I believe that you are using IDE. In this case it helps you to manage your packages. For example in eclipse you can perform right click and then choose New->Package.