Why can't Jython find my .jar file? - import

I have a jar file (not part of the Java standard library) that I want to use.
I've put myJavaPackage.jar into my project directory, and I'm trying to do import myJavaPackage, but I keep getting ImportError: No module named myJavaPackage. What am I doing wrong?

You need to make sure that Jython knows to search your .jar file for modules, which is done using pythonpath. This will differ depending on your setup, but in PyDev:
In the Package Explorer (the tree on the left), right-click on the project and go to Properties.
Select "PyDev - PYTHONPATH" on the left.
Click the "Add zip/jar/egg" button on the right.
Use the directory tree on the left to browse to the folder containing your jar, then click the checkbox next to the .jar file on the right, and click OK, then OK again.
You should also check that the module name you are trying to import matches the package name as specified within the jar file. Note that the jar file might be named differently to the package, especially if there are multiple packages within it.
If you have the source for the .jar, open up the .java file containing the code you wish to utilise, and look for a line near the top that specifies the package. If you find a line that says something like package foo.bar.myJavaPackage;, then you must do one of
import it like import foo.bar.myJavaPackage, and use the contents like obj = foo.bar.myJavaPackage.someClass, or
import it like from foo.bar import myJavaPackage, and use the contents like obj = myJavaPackage.someClass, or
import it like from foo.bar.myJavaPackage import someClass, and use it like obj = myClass, but careful of name collisions using this method.

Related

PyDev - Importing relative to project root name

Say I have a project named "abcproject". Is there any way to import like so in PyDev?
from abcproject.core.page import Page
from abcproject import util
As opposed to this, which works perfectly:
from core.page import Page
import util
If I add my project folder to my PYTHONPATH it will vaguely work, but PyDev will treat the imports as external and doesn't seem to update them properly as things change, which gets really messy.
Definitely.
The problem is that you have to set as a source folder the folder containing the 'abcproject' and not the 'abcproject' itself.
I.e.:
You need a structure such as:
/my_project
/abcproject
__init__.py
/core
And /my_project must be set as the source folder (in project properties > PyDev - PYTHONPATH > Source folders).

How to import a package from a linked folder resource in a Flash Builder project?

I have this folder structure:
/source/MapEditor where the main project resides. (with its src folder inside)
/source/Geonerd with some classes declared as belonging to the Geonerd package.
I've tried to include the Geonerd package, with no luck.
How do I import it? ( the yellow warning icon says Definition Geonerd.Polygon could not be found )
If I copy the Geonerd folder instead, inside the src folder, I can do import Geonerd.* correctly, but not with the linked folder.
Note: it's not a .swc library, it's just the .as sources inside a folder all within the Geonerd package.
Note 2: I added the folder to the project's ActionScript Build Source Path but doesn't work either:
Note that what should be the Geonerd Package is recognized as (default package)
Update: if I make the linked sources belong to the anonymous package, I can import them, but the problem is they have to be in their own package to avoid name clash with other libraries I'm using. And that libraries in turn are also in their own package and I need to import them as well.
You need to add it as an external folder.
Select Project -> Properties -> ActionScript Build Path. On the build path properties page, select the Source Path tab. Click the Add Folder button. Enter or browse to the folder, click OK. The folder is added to the source path.
Should look similar to: http://blogs.adobe.com/airodynamics/files/2012/05/snapshot1.png
Actually I had to add a folder that had a folder inside, named Geonerd so then Flash Builder recognizes it as the Package Geonerd and not just a default package.

Import .java Files Into the Correct Package

I have a bunch of *.java source files that contain classes in various packages. I am trying to import all of these files into Eclipse projects, but Eclipse always places them in the default package.
How can I tell Eclipse to place each of the files in the appropriate package subdirectory, so I don't have to create all the various packages manually?
I have tried the import from file system feature as well as copying the files into the project, all of which would place the files in the default package rather than those ecplicitly stated in the package statement on top of each file.
Related SO questions only deal with referencing packages outside of the project's src directory hierarchy, manually creating files in the suitable package directory, missing import statements, or bulk-renaming packages, but it doesn't seem like this problem has already been discussed.
Update: As requested in the comments, here are some example files:
Test1.java
package some.random.pkg;
public class Test1 {
}
Test2.java
package some.other.pkg;
public class Test2 {
}
Test3.java
package yet.another.pkg;
public class Test3 {
}
Place all of them in the same directory, then try to import them into a Java project in Eclipse without manually creating the directories for the various packages.
Update 2: In order to clarify the scale, assume that there are actually approximately 100 files like these (of course, with more contents ;-) ) on a weekly basis, which is why I'm looking for an automated solution.
I was somewhat bored, so I created a Java app to get all .java files from it's current working folder and put them in the appropriate subfolders.
You can then copy/paste the newly created folders/files into Eclipse.
Source code and runnable .jar: https://github.com/felipeweber/OrganizeJavaSources
To execute it you will copy/paste the runnable .jar in the folder you have all your .java files in, and run it with java -jar OrganizeJavaSources.jar from a prompt or command line.
It should work in both Linux and Windows (and probably MacOS too).
In my tests it worked perfectly with sane files.
Please note I haven't done any error treatments and so fourth, but feel free to contribute.
Good luck
According to this link you can get what you actually want to do.
File -> New Project/Dynamic Web Project
then
File -> New Folder, click "Advanced" and select "Link to folder in the file system".
After that right click on the folder and select BuildPath -> Use as source folder..
That is it..
OR
you can create a new folder, copy top-most folder containing *.java files and paste it. Then right click on it, select BuildPath -> Use as source folder...
I hope it works for you!!
Well we are programmers here right? so as far I do a research its kinda hard to find this kind of feature in any IDE's because a programmer must be warned that he put a java file in an incorrect package and must not automatically arrange it for the programmer.
Well I suggest you a create a simple java console program that can do the file manipulation for you and I know it would be easy for you. this will be the possible algorithm of the program
Read a .java file
Check the package line until the terminator ; on that .java file
relocate that file according to the package name, well replacing . with directory separators can be used as address where you will relocate the file.
Create the folder if the relocation folder of the file doesn't exist
Do this repeatedly until all files are relocated.
Well me also wants this kind of feature not only in eclipse but also on other IDE's well ill be waiting for further answers in this question.
You need a hack.
I hope you are using linux, otherwise is going to be more difficult :)
1- Create a little java program that reads and executes an .sh file
Here an snippet that shows how you can atemp to read from an script
ProcessBuilder pb = new ProcessBuilder("myshell.sh", "myArg1", "myArg2");
Process p = pb.start();
2- Create the .sh script and make sure java is picking it correctly(Just place some echo message in the script and make sure its being printed)
3- The goal with this hack is to try to use a grep that reads all the files with the .java extension and pipe it to the mv program which will put them in the correct folder.
4- The way you will find out how to put what in which folder(package) will be possible because you can read each of the files using cat and this way determine where they should go. The mkdir command should help you create the folders correctly before moving the files.
5- If you are not sure about this solution, let me just tell you that this is a highly complex task that requires you to examine well and recognize all the patterns. I am confident it is possible and that you can do it using a conditional piping logic and some nice relaxing chill-out music to help you focus.
Here I end you some BASH guide to refresh your mind
Good luck, and happy new 2013
When a file is in the wrong package, Eclipse will offer you to fix the error by moving the file. With the "quick fix" feature (select in the Error view, press CTRL+1), you can easily move them. Try selecting them all at once, maybe your Eclipse version is newer than mine and can fix them all in one go. If not, it is still faster than manually creating the package.
So this is what I did ...
New -> Java Project
(options: click "use project folder as root for sources and class files")
Next
Select "Link additional source" (under Details)
Browse "Linked folder location" and select the top folder (containing all your packages etc.)
Finish
If you already have a Project created then ...
Right-click on project in package explorer and select Properties
Select "Java Build Path"
Select "Link Source" (on the Source tab)
Browse "Linked folder location" and select the top folder (containing all your packages etc.)
Finish
If your files are not in directories corresponding to the packages defined within them, then TAKE A BACKUP OF YOUR FILES (i.e. copy them all to another directory) and try this:
Compile the following program with javac MakePackDirs.java and run using java MakePackDirs and directory and file structures will be created according to package declarations and then use procedure as described above.
import java.util.regex.*;
import java.util.*;
import java.io.*;
public class MakePackDirs {
public static void main(String[] args) throws Exception {
String pkg;
for (File f : new File(".").listFiles(
new FilenameFilter () {
public boolean accept (File dir, String name) {
return name.endsWith(".java");
} })) {
if (f.isDirectory()) continue;
System.out.println("Processing " + f.getName());
Scanner s = new Scanner(f);
try {
Pattern p = Pattern.compile("^[ ]*package +[^;]*",
Pattern.MULTILINE);
if ((pkg = s.findWithinHorizon(p,0))==null) continue;
}
finally {
s.close();
}
System.out.println("Found " + pkg);
pkg = pkg.split(" ")[1];
pkg = pkg.replaceAll("\\.","/").trim();
System.out.println("Creating " + pkg);
File dir = new File(pkg);
boolean created = dir.mkdirs();
if (!created && !dir.exists())
throw new Exception("Can't create package directory");
// Moving file
File dest = new File(dir, f.getName());
boolean renamed = f.renameTo(dest);
if (!renamed)
throw new Exception("Can't move file to " + dest.getPath());
System.out.println(f.getName() + " moved to " + dir.getPath());
}
}
}

Eclipse confusing package to be a folder

I have read some questions on this around here but my tries at the answer have not yielded success. Here is the problem:
Package:com.me.a
Folder(under above package):b
Java class(under above folder):Myclass.java
The proper package structure is com.me.a.b, but eclipse thinks 'b' is a folder. When I right click on b and go to build-source, there is an option that says "use as source folder" which then does something totally unwanted - creates a separate folder with the entire package path as a source folder, and uses a default package:
Folder: com.me.a.b
Package: default package
MyClass.java
How do I solve this annoying issue?
PS: I am unable to use images which to give a better idea but being new here, there is a restriction.
You have project/com/me/a/b set as the source folder. Your build path looks something like this.
You need to set project as the source folder. Your build path will look something like this afterward.
this is very strange. packages are basically folders, so saying that you have package com.me.a with a folder in it named b is essentially the same as having package com.me.a.b.
Or there is something wrong with the Eclipse's workspace metadata for this particular project?
Try creating new project with such package and class in it see if it is any different?
The behavior you describe when choosing "b" as a source folder is expected: you are basically telling eclipse that this folder b is a source folder, a root folder for source java files. this is the default package, every sub-folder of this one is considered a different java package...
I just had this same problem with ONE folder between about 100 folders. The solution turned out to be righ click on the folder (in the package view), choose "Build Path", and then "Include" (Instead of 'use as a source foldre' as the OP did). After that the folder turned into a package.

Using .class methods in Eclipse

I have two classes, Phone.class and Time.class. I would like to use their methods for my project in eclipse, but I cant figure out how to do it successfully. How do you import the .class files so that I can use their methods in eclipse?
Add the folder containing both classes to the build path for the project: right-click on the project in the Package Explorer window and select the Build Path option, then select the Configure Build Path option. The Libraries tab has Add Class Folder and Add External Class Folder options: use the first if the folder with the class files is already part of the project, otherwise use the second. Select the folder and then hit OK and you should be good to go.