Java import directories - import

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.

Related

Katalon external library

I'm trying to use my own code for Katalon as a library.
My own code has imports like this
from com.test.page import Page
from selenium.webdriver.common.keys import Keys
import robot.utils.asserts as asserts
This file is totally executable on its own when I'm using IDE, but when I tried to import it to Katalon, I got exception message like this
ImportError: No module named test
In order to use external references, you should go to the IDE and select
Project -> Settings -> External Libraries
On the window add your jar file.
Remember that you also need to check if the .classpath file is updated accordingly if you use git (it's on the .gitignore list, so needs to be updated manually)
Do you put your .jar in Drivers folder inside your project?
Write import com.test.page.Page instead of yours
The best place to keep all jar files is to create a folder in Katalon folder structure and store all jar files in it. When you commit Katalon project into a source repositories like Github and SVN, don't forget to commit jar files as well so that the references given in the code will no throw an error.
Try this:
put additional jars in lib directory like "C:\Users[ME].katalon\8.2.0\Katalon_Studio_Engine_Windows_64-8.2.0\configuration\resources\lib\jsch-0.1.54.jar".
restart katalon then in your script, use import com.jcraft.jsch.*;

I am not able see the jar package structure,but there are no errors

I have following import in my class
import org.apache.commons.lang.exception.ExceptionUtils;
ExceptionUtils is indeed referenced in my class.
JOptionPane.showMessageDialog(this, "An Server Error occured during saving.\n\n" + ExceptionUtils.getStackTrace(e), "Error",**
But in the classpath i have package org.apache.commons.lang3
I am really not sure how this runs and why there are no errors?
I am using Eclipse Indigo
Please advice.
It sounds like your Apache Commons Lang .jar file satisfies your org.apache.commons.lang.exception.ExceptionUtils import.
Look here:
http://www.java2s.com/Code/Jar/a/Downloadapachecommonslangjar.htm
STRONG SUGGESTION:
Take a look inside of your .jar file. One easy way to do this is with a tool like 7-Zip, or jd-gui. Another way is to temporarily rename the file from .jar to .zip, and look at its contents in Windows Explorer.
This will show you the packages (directories and subdirectories) and classes inside of the .jar.
Interesting stuff - try it sometime!

Importing classes & utilities in the manifest // JNA

I'm a CS student and am in way over my head for an end of the year project, for which I need to find the dimensions and locations of a separate window. I've found that I need to use JNA libraries and understand how to do that but I don't understand how to import the JNA libraries through the manifest. Here is my file structure so far (I'm working in netbeans):
Project Folder
dist Folder
nbproject Folder
src Folder
Test Folder
Build.xml
Game.exe <-- This is the exe I run from the java file itself. I need to find this window's size and location for use later in the project
jna.jar <-- need to implement this as a classpath
Manifest.mf <-- I supposedly need to edit this.
The reason I show you this is to ensure that I've put the JNA Jar in the correct place.
Next, here is my manifest:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Class-Path: C:\Users\justi_000\Google Drive\4. APCS\JZMinesweeper\jna.jar
So I need to edit the bit after the java -cp but I don't know what to put there, and in what order. The java tuts, etc aren't of much help.
Additionally, are these imports correct? They may be the problem and I have correctly imported everything, but I doubt that.
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.*;
And, turns out, that it was a stupidly easy solution that my general incompetence caused me to miss.
Turns out it is actually possible to directly import libraries in IDEs...probably why they all include a tool for it. Blind old me has only ever used them once before (the perils of being a student) but we're all set. Simple as right click libraries>add jar.
Figured i would stick this here for future reference.

Eclipse IDE won't resolve classname automatically

I have a Jar, which I have included in my project.
When I import a class from this project by specializing the full name as org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor the class is imported well.
Writing ICellEditor and doing "Organize Imports" doesn't work.
Doing "Open Type" and typing ICellEditor finds the right class, so Eclipse is aware of this JAR amd class; but does not show the class on "Organize Imports".
Do someone know what the reason could be?
Make sure that your jar path is included in the CLASSPATH. When you give fully qualified name as shown below CLASSPATH is not required. (as you give the complete path explicitly).
org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor
So if you want your import in eclipse to work properly :
So either put this jar in the existing jar directory or make sure that the jar path is added in CLASSPATH.

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

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