Using a jar internally in Python | OSX Issues - java

I'm reading pdf files with python. And in my script, i'm calling a jar file by command line using os.system.
Two things here:
OSX annoyingly keeps redirecting me to desktop everytime the jar is called. I can optmize that, but I don't want OSX to do that; and
Isn't there a way to no actually open java internally ? Like attaching the code inside my python script. Let's just say I don't want the user to see java being opened.
Thanks in advance.

You may want take a look at similar discussion here. To summarize the discussion, you can use Py4J.
There is also another project called JPype, however that seems to be a very old implementation (way back in 2009) and no updates thence.

Related

Export a JavaFX project in eclipse to a runable jar

I need to send a project made by JavaFX SDK 16 and Java SE-15 using Eclipse to my friends, but when I try to export it, i'm getting surprised that Eclipse doesn't insert the VM arguments to the exported file :
and as you know since JavaFX SDK 10, JavaFX libraries are not anymore part of the Java SE, so we need to tell Eclipse where to find them by VM arguments, in short words, these VM arguments are really important to run my program.
I already know that I can insert those arguments while using prompt command to open it, but is there a more simple way? just by clicking on the exported file ?
Your main problem is that you are trying to do something that does not make any sense. Runnable jars are not a proper distribution format for JavaFX applications for various technical reasons. The way to go is having a look at jpackage. That's the proper way of dealing with distribution nowadays. Otherwise your are just banging your head against a wall.
Here is a good summary of all the options you have: https://stackoverflow.com/a/68823040/4262407
I have this problem 5 years ago (I use netbeans IDE, so I don't know if it works in eclipse, but I think yes), and I cant find a good way to do this.
Instead, I created one Swing application, and put all code of JavaFX inside.
You could see how I do in
this link from Oracle.

How do i get the working directory of a windows process in java?

I would like to get the path to the working directory of a specific process (for example for the PID of the process). I am Not Talking about the working or current Directory of the process where my Java Code is running. Its a simple task with Linux, but for Windows i cant find a proper solution. Furthermore, it would be nice, if its a Command or a Framework for Java, because i will need the path in my Code. I am not looking for the path to the executable, also Not for a solution with wmic or process explorer.
Already thanks for the help.
I already tried commands like tlist and wmic, but those solutions cant be utilize in my code. I am looking for a solution that i can use without special installations on Windows.
JNI and JNA provide means to call directly into native libraries from Java code, and it is feasible to use these to call out to Windows libraries.
There is a github project that appears to be close to the need: https://github.com/kohsuke/winp. Perhaps you can add the needed code and send up a pull request, or fork the project.
Note that any solution here is going to be windows-specific, meaning the application using it will not run on another platform. Given the nature of the question, that doesn't sound like it would ever be a concern.

How do I make a java .exe to allow user to interact as I would in the eclipse command window?

I wrote a simple java code that would take simple inputs from the user in the command window (of eclipse for me) using nextInt() and nextLine(). However, I realized that others need JRE (I believe?) on their computer to run the executable jar file made. So I was wondering if there is a way to get around that by making the app produce a window that is like the command window to have the same interaction as the command window in eclipse.
So, if I were to run the .jar or .exe then a simple window would pop up that acts like the console of eclipse displaying lines from System.out.println() and etc.
To run a java program you need the jre. There is no way around that.
If you need the console, nothing is stopping you from running the java program from the windows command line, which will do exactly what you ask for.
You still need the JRE.
Unfortunately, when starting to learn Java with Eclipse, many people miss the opportunity to at least start to understand how to do the same from the command line, which is, if you ask me, good to know.
For programs written in Java, they are compiled as a jar file, like you mentioned, and how these compiled versions of your source code differs from many other programming languages is that they do not contain the assembly/machine code like for example a compiled C program would have. They are instead compiled as bytecode. Which is special code for execution by a Java Virtual Machine. Here is a good Wikipedia reference: link
To answer your question, yes, others need a JRE (Java Runtime Environment) and this can be either:
Installed by themselves (this is what you mentioned)
Packaged together with your java app, to provide a download-and-click experience.
For option 1, assuming they already have it installed, they can simply run it by executing the jar file with javaw, more information on that is in this previously answered SO question
For option 2, the process is fairly lengthy and I'll point you to the official docs to refer to: self contained executables and Deploying java apps
If you have a more complex project with third party libraries and what not, look at this SO question
In the past, I've also found launch4j, a cross-platform wrapper to be very useful, it automates the process of going from jar to an executable (made a simple game that using Swing, simple and ugly thing it was), but the user still needs a JRE, nonetheless. :)
Hope this helps!

How to put java code into an application format?

I made a simple command-line based game in java, only two classes (using Eclipse). But I was wondering how I can make this into a usable application for anyone, without running it through eclipse (ie send it to someone who knows nothing about java but would still be able to play the game)? Thanks!
You want to create a runnable jar file.
Eclipse has an option for this in the "Export" menu. For more options, search for "executable jar file" here or on Google.
You want to make sure that you also include any jar files your code depends on as well (Eclipse can also do that for you).
Users will be able to start this by double-clicking on the file on most platforms. If you need better integration (such as a custom icon), you will need to bundle it up further into an OS-specific executable. But for starters, a simple runnable jar works fine.
send it to someone who knows nothing about java
You need to get them to at least install the Java runtime on their machine (if it is not already there).
Just to be clear, "command-line" and "knows nothing about java" are probably not going to work very well for you given that:
java is OS agnostic, therefore, if you send (presumably) a jar file to say...your grandma and she has a mac and you have a PC chances are her getting it to work is not going to be "out of the box easy" so to speak.
Left with this, I think you have a couple choices...first off, you do need to package your classes - a runnable jar will work fine. Aside from that, you will most likely have to build OS specific scripts (batch scripts for Windows, shell scripts for unix, etc.) and you will have to hand these out with your jar file. That being said, the intended user will still need to have java installed, and the batch scripts themselves are not likely to be trivial endeavors.
Your next option would be to use JNLP. However, I don't think JNLP has a command line mode, so you will likely have to simulate a console with something like a JTextArea.
As far as I see it, your last option it to use one of the many products (not sure if there are any free ones) that package java into native code. I think Exe4j is one such example - but, like I said, I am not sure if there are any free ones and I am not sure how hard they are to use.
Best of luck, and if you can't get your jar to work you should probably move that to its own question.

Change Directory in MATLAB from Terminal/JAVA

I need to be able to change the working directory in MATLAB without interacting with the command window. I'm launching MATLAB from a Java application. Right now the only solution I've come up with is closing MATLAB, changing directory from JAVA and relaunching. Is there some streamlined way to send MATLAB the 'cd' command from JAVA? Doing so from the command-line would also work, since I could use getRuntime().exec(command)
Thanks!
You can do this using JMI if you're using the same JVM as Matlab (if not then I have no idea). There is not much online info about this (it's WAY undocumented/unsupported). Google it or read this: http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. In short, you need to include Matlab's relevant JAR file and then use com.mathworks.jmi.Matlab's functionality. For example:
Matlab.evalConsoleOutput("cd('C:\Program Files\')");
Yair Altman
http://UndocumentedMatlab.com
I am working with Stephen Poletto who posted the original question. There wasn't any existing solution that met our needs so we wrote our own solution based off of Kamin Whitehouse's work mentioned by Yair. It is available for all to use at matlabcontrol.googlecode.com
It allows for controlling MATLAB from a Java program launched outside of MATLAB.

Categories