How do I get the Jython PythonPath to reliably include java class paths? - eclipse-rcp

Question
Is there a way to reliably ensure that both the python source paths and compiled java class paths are added to the Jython PythonPath when running unit tests under eclipse?
If not, is there a better workaround than adding absolute paths to the PyDev PythonPath, External Libraries list?
Background
I have been having a strange problem with running Jython unit tests on a Jython class which inherits from a Java class.
We have an eclipse RCP application with a structure (simplified) like this:
Config project (Pydev nature only)
scripts
src
classes.py
test
classesTest.py
Core project (Both Java & Pydev natures)
scripts
src
baseClass.java
classes
baseClass.java
Library project (Both Java & Pydev natues, but no Jython scripts)
src
interface.java
classes
interface.class
In classes.py I import baseClass, which implements an interface defined in interface.java. I then create a Jython class which inherits from baseClass.
In my classesTest.py, I import classes and define the unit tests.
The problems come when I try to run the unit tests using "Project explorer > classesTest.py > Run as > Jython unit-test":
Sometimes it is unable to find the baseClass (I think it fails with an ImportError, but I can't reproduce this right now).
At other times it finds the baseClass, but then can't find the interface, so it fails with a java.lang.NoClassDefFoundError.
If I pprint the sys.path at the start of the test module, then I can see that:
While core/scripts is on the python path, neither core/classes nor library/classes are on the path.
Both core/scripts and core/classes are on the python path, but library/classes still is not.
Sometimes, very rarely, both classes paths are added to the python path and the unit tests work fine, without the workaround below. Unfortunately, I haven't worked out how to reproduce this and can see no difference in the files in the source tree between it working and it not working.
Current Workaround
My current workaround is to explicitly add in "External Libraries" for library/classes and core/classes explicitly. Unfortunately it appears that these have to be absolute paths, which means I have to set them differently for every trunk, branch or computer that I use. As such, I don't want to commit these into revision control and it is a pain having to set them up every time.
Notes
Note that the Config project only references the core project, but referencing the library project explicitly makes no difference to the class path.
Also, while trying to find a reliable method to reproduce this problem, I had other libraries inexplicably disappear from the python path and then just as mysteriously reappear, the most recent example being Mock!
Update
I haven't seen this problem recently (currently using Eclipse 3.7.1 & PyDev 2.2.2) so it may have been an aberration with the combination of Eclipse/PyDev I was using at the time.

For a java project, you have to explicitly add the folder with the .class files to the PYTHONPATH.
The recommended organization would be:
java_project/bin <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH).
java_project/src <- this folder should be added to the jdt classpath, but should not have any reference in PyDev
python_project/src <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH).
And the python_project should have a reference to the java_project.
See the last part of http://pydev.org/manual_101_project_conf2.html as a reference.

I had a similar problem. Recently resolved by accordingly modifying the python.path field in the Jython registry file.
It looks like the Pydev plugin cannot modify the sys.path to be able to import the Python module from a Java project in Eclipse.
More details at Adding in a python module to the sys.path for a Java project in pydev, Eclipse

Related

Define a Java 9 multi-moduled project in Eclipse

I'm trying out Java 9 Jigsaw module system (no module experience yet) and would like to use it for capsuling the classes within my project, but it's confusing.
According to this article it should be possible to have multiple modules within ONE project. I made a new project in Eclipse Oxygen (Java 9 is supported) with the same structure as shown in the article. But Eclipse keeps telling me that I must not have more than one module-info.java in a project.
I really don't know how to tell Eclipse that it should use the "multi-module-mode". And I really would appreciate not having to create a new project for every single module.
This works:
This not:
But according to this article something like that should work:
And how about deployment of a modularized project with Eclipse? There is nothing to see about the new jmod extension. Do I still export it as a runnable JAR file like before?
Notice that my questions refer to working with the IDE (no command line, I mean with an IDE that should be possible, right?) Thank you for enlightening me.
Currently, Eclipse requires you to create a separate project for each module (e. g. because each module has its own Java Build Path).
To understand this design decision, consider that Java modules correspond to OSGi bundles / Eclipse plug-ins and it has always been to have a separate project for each bundle/plug-in. If you come from the Maven world, you would probably expect a deeper folder structure instead. But modules are self-contained and combining several modules into one project would only add an additional folder level without meaning. However, Eclipse supports nested projects and so-called working sets if you need an additional folder level.
Exporting modules as images is planned for Eclipse 2019-03 (4.11), on March 20, 2019 (see Eclipse bug 518445). Exporting modules as JARs that can be used on the modulepath (-m) already works (see my video).
I don't know if this question is still open for an answer, but you can solve this problem by simply removing all source folders on the build path. At least this works for Eclipse 2021-12 version.
As you can see this is a demo project from the Official Gradle Guide Book and it has multiple modules. Each module has its own module-info.java.
project structure in IntelliJ IDEA
If I open this project in Eclipse it will give me the 'duplicated entries on module-info.java' error.
Eclipse shows the error
But if I delete all the source folders on the build path, the error is gone and the project can be built and run without problem.
project properties: Java build path
The only problem is that you have to build the project with Gradle so that it will produce the .jar of each module and you have to include them in the libraries later.
include all the .jar in libraries
I think this is probably the same solution mentioned by howlger above.

Java import directories

I want to ask smaller question about Java VM and thing about it.
If I understand import process correctly, then when I request contents of packages by import keyword in Java, packages are being imported from it's specified import directory. Then when I'm running Java application from a directory in computer and some of the imported packages could not be found in JVM import directory, JVM is looking for them in current directory. But in that case something must be wrong, because when I run my project from Eclipse and I request some package from the project, it's found. I think that Eclipse is changing directory where packages are being searched. Am I right? Is this all way of importing in Java?
The import statement is just syntactic sugar so you don't have to specify the fully qualified name everywhere in your classes. It doesn't actually import anything in terms of code.
Classes are looked up in the classpath.
In eclipse you can configure the build path (for each project) option project - - > build path.
There you can either add a directory, jar file, external jar files, etc.
Also you can check what's already defined.

How to build a scala application that was created in eclipse scala plugin FROM THE CL

I have developed a scala application for the first time, but I have to deploy it with a "one-click" type script that can run and build the scala application from source WITHOUT ECLIPSE.
Since I'm completely new to scala I don't know how to tell it where all my source files are etc... to get it to build my app from the command line. I also have 2 3rd party .jar libraries that I need to tell the scala compiler to link to...
Any documentation on this? Or example command lines? My project hierarchy is:
src/packagename: contains all .scala
bin/packagename: contains all.class files
libs/ -> contains 2 .jar files I will need to import somehow
I'm working on debian linux
EDIT: I found this ability to export in eclipse so I created a .java file and called my main scala object from it. Then I exported as a runnable jar. However, when I go to run the new runnable jar "sudo java runnable.jar" it says "class not found exception: runnable.jar"
You should take a look at https://github.com/harrah/xsbt/wiki which is the common way to build a Scala project. Run through the tutorial in the wiki to learn how you should organise your directory structure, so that everything may run fine.
If you want to combine it with eclipse, checkout this plugin: https://github.com/typesafehub/sbteclipse

Eclipse: script compiler as part of a project

This question is not limited to lex and yacc, but how can I add a custom script compiler as part of a project? For example, I have the following files in the project:
grammar.y
grammar.l
test.script
The binary 'script_compiler' will be generated using grammar.y and grammar.l compiled by lex, yacc and g++. And then I want to use that generated script_compiler to compile test.script to generate CompiledScript.java. This file should be compiled along with the rest of the java files in the project. This setting is possible with XCode or make, but is it also possible with Eclipse alone? If not, how about together with Maven plugin?
(I might setup the script compiler as a separate project, but it would be nice if they can be put in the same project so that changes to the grammar files can be applied immediately)
Thanks in advance for your help!
You can add a custom "Builder" from the project properties dialog. This can be an ant script (with an optional target) or any other script or executable.
There are also maven plugins for ant and other scripting languages
If you just want to run an external program in Maven this is what you want: http://mojo.codehaus.org/exec-maven-plugin/ -- you can then run Maven targets from your IDE or command line and it should do the right thing either way.
To integrate with the normal compilation bind the plugin to the "generate-sources" phase and add the location where the Java files are generated to the "sourceRoot" option of the exec plugin. That way the compiler will pick them up.
Ideally you generate the code into a folder "target/generated-sources/MY_SCRIPT_NAME". That is the standard location for generated sources in the Maven world and e.g. IntelliJ IDEA will pick up source files inside of that location. Note that this doesn't work if the files are directly in "target/generated-sources".
The other option is to write your own Maven plugin, which is actually quite easy as well. See e.g. https://github.com/peterbecker/maven-code-generator

Netbeans project to scripted build

I'm trying to convert a Netbeans 6.9.1 project into a scripted build (without netbeans). Of course, it fails (or I wouldn't be asking for help).
In the failure it says that the org.apache.commons.httpclient package does not exist. (Of course, it worked when we ran the build in Netbeans).
Now I know exactly where the commons-httpclient.jar file is located in my project structure, but I can't seem to tell it to the compiler via the ant build files and the netbeans property files.
Perhaps related to this is when I ran "ant -v" to build my software, it said,
Property lib.mystuff.classpath has not been set. This variable is important, I guess, because
the file nbproject/project.properties uses lib.mystuff.classpath in its definition of javac.classpath, which of course tells the Java compiler where to find the JARs.
So...when moving a Netbeans project to a netbeans-independent scripted build, how can the build script set these properties? Also, how can I ensure that the jar file gets included in the ant build?
I appreciate any help I can get, as I am a Java newbie.
UPDATE AFTER ACCEPTING ANSWER FROM vkraemer:
There are a few best practices for build scripts for production software:
Put everything needed for a build under a single directory tree. (Netbeans = fail)
Put everything in source code control. (I did that)
The first line of the build script should clear all environment variables.
The next section of the build script should explicitly set all environment variables to values which are known to work.
The next part of the build should be able to execute using command-line programs such as javac, ant, cc, etc, and must not depend on firing up an IDE such as Eclipse or Netbeans.
It is a shame that Netbeans makes this hard.
I did a quick look in a Java Application project and found the following...
javac.classpath = ${libs.MyStuff.classpath}
libs.MyStuff.classpath is defined in %HOME%/.netbeans/6.9.1/build.properties.
You may be able to get by doing the following...
ant -Dlibs.MyStuff.classpath=c:\a\b\c.jar
You would need to do more if you have multiple jar files in the MyStuff library that you created in NetBeans.