how to import .py into java - java

I want to make a web application for books using java servlets with Tomcat. I want to import some code (that reads *.epub, *.pdf, *.doc, *.mobi, and *.txt files) from the project Calibre into my project, if it's possible.
Calibre source: http://bazaar.launchpad.net/~kovid/calibre/trunk/files Some source from this link I want to work into my project.
Application Calibre is an open source written in Python and C. What I want is to import sources written in python (.py), which allows me to read standard epub, pdf, txt ... in my project developed in java servlet to view and to convert the books. I using Eclipse(Java EE) and I do not know what steps to import these sources and how to make. It must to export these source to jar?

Python isn't compatible with Java, in that Java can't run python code.
But, there are a couple of options.
Run the python code from your java app directly with a system call.
I use ProcessBuilder for these situations, and you can look at
http://javaevangelist.blogspot.com/2011/12/java-tip-of-day-using-processbuilder-to.html.
Set up Calibre as a web application and call the conversion
functions from your web application to the calibre webapplication
you start up. It appears ready to run as a web application
according to http://calibre-ebook.com/about and
http://www.techrepublic.com/blog/opensource/how-to-use-calibre-to-access-your-ebook-collection-online/2275.

I see two other ways:
You could work with Jython (but I have no experience with that and don't know if it solves your problem)
You could write a JNI module for Java which in turn starts and uses the Python interpreter.

Python and java are different languages. You cannot have java code call python code or the other way round without some sort of conversion. I think your best option is to either
Look for a Java library that does what you want, or
Write your website in python; there are many excellent libraries for these
Sure there are ways to call python from java, but it'll be rather hard to apply them to something as big Calibre, which also uses some C.

Related

How to call Java methods from my jar file inside the Python Scripts

I am writing the python module where I need to interact with java module for some work.
I have already jar for java layer.
I can I invoke the jar files and call the class/Methods which are there in Jar files.
I don't want to use the Jython since major of my code is pure python
I tried subprocess.call()
but it's not serving my purpose
subprocess.call(['java','-jar', 'my.jar'])
EDIT:
I need to call the java layer because I need some input to my python module from there.
I tried py4j but no successes
JPype is an alternative to Jython that I made some good experience with. If it is not enough to call the java program and work with the output (it's hard to tell from your question), then JPype can be used to (more or less) transparently work with Java object in Python code.
I works by starting a JVM and handing requests to said JVM.

How to package a python script with my java program?

So, I'm writing a program(mostly for practice, kind of for future convenience) that will show a java front-end when run, and depending on the input, will use a python script to crawl and interpret a webpage, then send the interpreted data back to java to format for the front end. I'm sure I can get this running on my own computer, but when I want to distribute my program for my friends to use, how can I ensure my java program, mainProgram.jar can find my python code, script_x.py. Is there a way I can assure my python script will be supported. I've read that there are ways to include the necessary runtimes and such with your program when you build it, but would it still interact the same way with my java program if it was bundled with it's dependencies? I can provide some sample code soon, but it's still a work in progress, as I don't have the python script finished yet.
here is a link https://bytes.com/topic/python/insights/949995-three-ways-run-python-programs-java
use Jython (compiles to java byte code) https://en.wikipedia.org/wiki/Jython
distributing Jython applications: https://wiki.python.org/jython/JythonFaq/DistributingJythonScripts

Can I create a jar file with JavaScripts

Hi I'm trying to create a .jar file to include in Jmeter. Can I create a Jar where the code is written in Javascripts.
Technically, yes, you could, but it would require you to use a JVM Javascript engine like Rhino or Nashorn (supposed to be officially ready at the end of 2013). The running code in jars is contained in .class files, which are the executable "machine code" for the JVM. The most common language to write these in is Java, but many other languages can be compiled into .class files.
Javascript has essentially nothing to do with Java, and while you can use a program that runs on the JVM to run Javascript, I don't think it's quite what you have in mind. This Javascript looks basically like Java with the quirky Javascript syntax, and you still need to be familiar with the Java APIs that you're wanting to use.

How to write a Java program which will compile, load, and use code from the web

I'm looking to write a Java program which will download a Java source file in text format off the web, compile it, load it, and use it as part of the running program. I've heard this is possible, but don't know how to write the code to make it happen. A fully functioning example or tutorial would be great, if you could point me in the direction of documentation such as this.
Once I learn how it's done, I plan to use this knowledge to build an Android Application which can customize itself with code from the web.
A desktop program could use a shell script to download, compile and run a Java program. Android does not have a compiler, and adding one is non-trivial. The easiest way would be to also make a server program. The Android program would then tell the server program to download and compile the Java source code, and then send the result to the Android program, which would then load it using its ClassLoader.
One caveat is that the JDK compiler produces bytecode for the standard Java Virtual Machine, whereas Android's JVM is uses the Dalvik VM, so when you compile the Java class, you can't just use the JDK; you have to use the Android SDK to produce compatible bytecode that the Android ClassLoader can use.
Yes, it's possible in the general sense to do what you want. In the specific case of Android, however, it is likely that the sandbox imposes restrictions that will make what you're trying to do difficult or impossible. To do this in the general case you can use an approach like:
Use a web library of your choosing (for example, HttpClient, or HtmlUnit, or for simple tasks, Java's built-in URLConnection class is entirely acceptable as well) to download the Java file locally.
Use System.exec() to fork a javac process to compile the downloaded Java file for you (or use a JavaCompiler implementation to do the same). Note that this might be a bit tricky if the downloaded Java file uses external JAR's/libraries that aren't on your system.
Use the ClassLoader to load your compiled class. Note that you'll only be able to use it if your runtime classpath also includes any external JAR's/libraries that the code you're loading in relies upon.
However, step #2 will almost certainly not be possible on an Android device. Instead you'd need to compile Android-compatible class files somewhere else (like on a server, as Yusuf suggests), and then download and load the compiled class files from your app. Or, if you're really looking for a challenge, perhaps you could package a full Java compiler in your app, and compile the Java file(s) that way.
What you are trying to do is something similar to either the JRuby-for-Android project or AIDE both of which builds on device, but only in the case of creating apps to be run on Android as opposed to adding code functionality to an currently running app. While JRuby-for-Android is more for scripting, it may provide enough functionality as it is open source and you may be able to modify it to fit your needs.
The AIDE project appears to be more of an achievement as you can write in Java on device to build an app. Features include Dropbox and git support. AIDE appears to be a closed-source app.
Permissions will be a problem. You can't write files to the partition where the app is installed. Maybe you can if you move the app to the SD card.
An alternative is to create a custom classloader and use that to feed class files to the runtime. I don't know if you can do that on Android.
Is scripting an option? I'm sure you can find a scripting interpreter for Android. Then you can download and execute the script in the interpreter.

Wrapping Lua API in Java

I'm trying to write a large scale project in Java/Scala(a JVM language) that extends a preexisting program, but the problem is that the API is written in Lua.
I have found a list of websites that claim to be able to access Java from Lua and Lua from Java:
http://www.keplerproject.org/luajava/
http://code.google.com/p/jnlua/
https://www.github.com/dafrito/jna-lua
The program in which my project is extending, works by loading a certain script within a file. Instead, I want to run everything from a JVM project.
In other works: I need to be able to call functions within a Lua file that is loaded via a reflection-like system from a java project.
Has anyone done something like this before? Is it possible? Would you recommend a certain library for Java <-> Lua connection? Would you recommend an alternative?
Thank you for your time!
You might try LuaJ or Kahlua. I have used both, and they work. LuaJava works as well as jnlua. I know projects using both though I don't myself.
So you have 4 to pick from. There isn't a "best" one, each one has some pluses and minuses. It really depends on what you want to do.

Categories

Resources