Integration of Java and Python Code in One Eclipse Project - java

I am writing a compiler in Python using Eclipse with PyDev. I've come to a stage where I needed to write some code in Java. I'm wandering if there is a way of combining these into a single project, because at the moment I have two separate projects, and whenever I need to change the Java code, I have to manually copy the .class file into the Python project.
If this is not possible, what would you suggest is the most elegant way of structuring the files of these projects, and how should I set up my build process?
Thanks.

You can keep things in separate projects without having to copy the .class files to the Python project provided that you make that project a PyDev project and add the place that contains the .class files (i.e.: bin folder) as a source folder in the PyDev configuration (and reference that project from your Python project).
Take a look at "Project reference for Jython users" (in the end of http://pydev.org/manual_101_project_conf2.html).
I think you could also do what you asked in your question: create a java project, set it as a PyDev project too (right click that project > pydev > set as pydev project) and configure the PyDev project properties setting the PYTHONPATH to the place where you have your Python files and add another entry to the bin folder (i.e.: where the .class files exist). Not sure if it'd be better to keep those as 2 separated projects though.

I think Jython would be ideal here, as the Python code essentially gets translated to Java bytecode and run on the Java virtual machine, making it easy to communicate between the two. Also, pydev itself integrates well with Jython.

Related

Import Java Project from Eclipse to NetBeans [duplicate]

Is there a way to share the same *.java files between Netbeans and Eclipse?
You can import an eclipse project into netbeans,
or you can create an eclipse project from existing sources.
Sharing the Java source files is not the problem - just put them into some source control system, and then import them in both IDEs. Usually the configuration, especially the build process, is the main problem.
In practice, the solution is likely that you maintain separate project configuration files for both IDEs, but use a common build script (e.g. ant) that is supported by both. The project configuration allows you to use IDE-specific features, while the build script makes sure that both environments produce the same output.
Of course. eclipse supports linked source folders. The sources files don't have to be located in an eclipse project folder.
Inside an eclipse java project, select New -> Folder, then select "Advanced" and check "Link to alternate location" (eclipse 3.6, may be slightly different on other eclipse versions). "Browse" to choose the source folder from the netbeans project, press Finish.
Now you have linked (standard) folder to the Java files inside the netbeans project and modifying those files will modify them at the remote location.
Finally - right-click on this linked folder and choose Build Path -> Use as source folder.
(I leave the other way round to the netbeans experts)
In the ideal case you have your code stored in a version controlled repository (SVN, CVS, etc.). Then it is not as big of a factor whether you are using Eclipse or Netbeans.
This article, taken from a NetBeans 6.2 version of documentation, explains how you can import an Eclipse project into NetBeans and work with both together. NetBeans will create new project files but will link to the Eclipse source files, allowing sharing between the two. I have not tried this yet myself, but may be going down this path soon. After switching to NetBeans just this week, noticed that a significant feature in Eclipse is not in NetBeans - that of being able to create different run configurations that are not incorporated and stored into the POM. At this point in time, I do not have a solution to the run configuration issue, but if you do, please update the SO question here.

Get back my java source code from .war file?

I recently lost my netbeans project folder of the project that I was working on at the time. However somewhere on a server here at the company that I work at, I deployed it. So that means that I (hopefully) can still get hold of the .war file.
Is it possible to then 'unpack' this .war and get my .java source files back that I was working on at the time?
Thanks in advance!
If the .java sources aren't in the WAR (and they should not be), you'll have to take the additional step of decompiling them using a tool like JAD.
This is a good time to set up a version control system, like Subversion or Git or Mercurial, so this never happens to you again. Get into the habit of using source code management, even if you're the sole developer working on the project.
Short answer: No.
Slightly longer answer: Look for Java decompilers, but they won't give you your Netbeans project folder.
You only get *.class files from your war (rename war to zip and use a decompression tool).
Then you could decompile them.
See this related question for some suggestions.
Suppose if you had exported the source files while creating the war, you can get it. Else, JAD is your only hope that too cannot fully rely on it.
It is possible to unzip the war file where you will get only class files and other property files. Then use Java decompiler to see source code and it works really well (not recommended).
Also you can change the property files without JD and all you need to do just change the property files and zip to war file again.It will work.
But I would recommend you to maintain source code in SVN or TFS or multiple copies with version numbers in local system at any point of time.
In Eclipse, you can Import your War, it will create a new Project and set the resources in the project structure. IF your war holds the source java files, the project will cointain your sources. IF not, the packages will be empty and you will have to manually decompile your classes from bytecode to java files, using jad or another decompiler.

Java eclipse directory structure and tomcat

I have a java application hosted on tomcat, which has a directory structure like :
webapps/my_aapp/WEB-INF/CLASSES/SERVLETS/xyz.java
webapps/my_aapp/WEB-INF/CLASSES/SERVLETS/xyz.class
so that's how classes are referenced .
Now (I am new to eclipse), it generates a different directory strucutre, in which all .java files are outside the code and I don't know where class files are stored.
The result is that when I upload the files, and compile, I get errors like this:
javac -Xlint servlets/Ajax.java
warning: [path] bad path element "/usr/java/jdbc7.2dev-1.2.jar": no
such file or directory
warning: [path] bad path element
"/usr/java/mysql-connector-java-3.0.16-ga-bin.jar": no such
What's the best way to understand this java directory path issue? Better yet, how to synchronize this structure?
I want to keep my eclipse directory structure on tomcat.
Typically eclipse stores all class files under bin directory located under your project near src directory.
I personally like name classes more than bin, so I always change it when I am creating project or later using project properties (right click on project -> properties -> build class path.)
Check your disk, I believe your class files are there. Unless you are using some pluging that can change this behavior.
What kind of project are you using in eclipse? A WEB project? An ANT-build project? MAVEN build? The way it builds classes depends on it. There is not much that I can help you. Because you do not provide enough detailed information with your question, but I will try.
Anyway, you can configure your output dir to be web/WEB-INF/classes (which is not statndard and not recommended, but I am doing it in one old project ;D) if you have it configured as java project.
1. Right click on project,
2. Go to Java Build Path
3. go to source tab, select source folders and select output folder (in my project mypoject/web/WEB-INF/classes ).
4. If you want to run and debug on apache tomcat from withint eclipse using this configuration, you can use eclipse sysdeo tomcat plugin.
Other thing is: You have specified absolute links to libraries (I suppose looking at your error messages), please copy them to web/WEB-INF/lib and change links in project settings. (If you are using maven that you should learn maven, if you are using ant, than just rewrite the part thaht uses these libs). And check if you already has this libs in tomcat/shared or tomcat/lib classes.
Another way is to learn to work with standard way of creating web-projects in eclipse, and add a build and package tasks to your build process (whatever it is make, ant or maven). Then you should deploy your PACKAGED application (let it be *.jar).
My suggestion, please, read "Head First Servlets & JSP by Kathy Sierra, Bert Bates, Bryan Basham" this is great book. It has everything you should know about HTTP, JSP, servlets, tomcat, and web-application directory structure. It will make things much easier for you (at least it worked for me, it really helped me much to understand things, and it is great fun to read).

Linking to generated Java protobuf code in Eclipse

Here's the workflow that I'm trying to build
compile my proto files using a script, putting the generated src in a specified directory
link to the generated classes in Eclipse
compile my project
I am easily able to do this for C++ using Eclipse CDT: In my project I choose File->New->Other and choose File under General. Then I click on Advanced and select link to system file.
I haven't discovered how to do this for Java, though. Once answer here suggests creating another project with the generated code and make that a dependency to my project. This works but seems redundant.
Is there a way to directly link the generated protobuf Java classes to my project?
Well Java doesn't really have a "link" phase. Your two options are really:
Build the generated code outside Eclipse, and then add a reference to the relevant directory or jar file
Include the generated code within Eclipse (e.g. by having a source path which includes the directory containing your generated code) and get Eclipse to build it along with the rest of your code.
I suspect that the first option will make it easier to keep the generated code well away from your real source, but the second option may make it easier to browse the generated source, and package everything up.

How do you use a Java Library?

I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.
I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.
If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?
And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?
I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.
Most java problems are classpath problems.
Have you included those libraries in your classpath?
If you are using eclipse, you could
Project - > properties -> Java build path ->addJar.
And the Jar file should be placed in a directory inside your workspace (lib/ for example)
If you have to take your project to another computer, you could take these steps
Before doing anything, export your project (as a Jar file, for example).
Save it into your favorite drive (cd / usb drive/ diskette/ tape).
On "the other" computer, you can import this project into your workspace
In Eclipse, you need to add libraries to the project build path.
In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:
javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java
These dependencies would also be required to invoke the app:
java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass
This page gives some good info, though googling for the term "classpath" should provide alternative sources.
You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.
You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.
Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.
Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

Categories