Windows can't run python script from within eclipse - java

I'm on a Windows machine and using Eclipse.
My java code is invoking a python script by using :
Process p = Runtime.getRuntime().exec("cmd /c e:\\dev\\CodeBase\\WebService\\src\\com\\rest\\service\\PythonScript.py");
On running the code it opens the prompt for choosing a program to run the script with. What can I do to make it run implicitly?

Try running Python interpreter instead:
Process p = Runtime.getRuntime().exec("[PATH TO YOUR PYTHON DIR]\\python.exe e:\\dev\\CodeBase\\WebService\\src\\com\\rest\\service\\PythonScript.py");
Usually [PATH TO YOUR PYTHON DIR] is something like C:\\Python27\\ or C:\\Python34 depending on your Python version.
If you have Python directory added to your system PATH variable sole .exec("python ...") will suffice.

You will need to convert the .py file to .exe use Py2Exe. You can download it and follow the instructions to convert it.

Related

Java\Python\InteliJ: Is there a way to debug a python script called from java code?

I have 2 projects: A maven based java project and a python project.
From the Java code I have a call to some py file in the python project.
I call it using a shell process creation. The command is something like this:
cmd /c "cd /d c:\Users\username\gitwork\lre-events\venv\Scripts & activate & cd /d c:\Users\username\gitwork\lre-events & python filename.py
To debug the python script I would halt the java execution right before calling this command, and then move to PyCharm and execute the command using debug configuration with the relevant arguments values.
My question is whether there is a seamless way to step into the python code from within IntelliJ directly when I call the python script?
I already installed the Python plugin in IntelliJ and imported the python project into it.
But I probably do not understand how to create the dependency and how to make the call so I will step into the python code for debugging.
Is Jython a valid option? (Update: maybe - but anyways not for me since I am using JDK 15 and Python 3.9)
Thanks.

Execute a shell script (shell script which uses perl and other shell scripts) in Linux from java

I am trying to execute a shell script (that makes use of a perl script and other shell scripts) from java program, however I am not successful, here is what I tried:
On the Linux server, in a folder test1/testJava the following scripts are available:
decode24.sh (the main script I call from java program, this script makes use of the other scripts listed below)
fram.sh
shadow.pl
light.sh
Here is what I try from java:
try {
ConnBean cb = new ConnBean("xx.yyy.zz.p", "user","passme");
ssh = SSHExec.getInstance(cb);
CustomTask ct1 = new ExecShellScript("/test1/testJava", "./decode24.sh", "arg1");
ssh.connect();
net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
}catch......
Result after execution:
error message:
./decode.sh[17]: shadow.pl: not found [No such file or directory]
./decode.sh[21]: fram.sh: not found [No such file or directory]
The error comes from your decode.sh script. It cannot find the shadow.pl and fram.sh scripts that it executes. Probably because the CWD or path is not set to the script dir when you run the script.
The most robust way to solve it is to change your decode.sh script to use absolute paths to the shadow.pl and fram.sh scripts. That way you can execute decode.sh from any directory.
If you use bash, check out this question for a neat way to resolve the absolute directory to the scripts to avoid hard coding the path.
Let decode.sh find out the directory it is in (see bash solution) and use the path for calling the others.

Making a "macro" command to run a program

I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!

Trying to run a java application from a shell script on Ubuntu 10.04

My java program was written on a windows machine and I am trying to get it installed and running on a Ubuntu 10.04 machine. I have created a .tar.gz file with myProgram.jar in it as well as 5 supporting library .jar files in a lib folder. Where do I put these files? Do I need to extract it on the Linux machine to a usr/bin folder? Does the shell script go inside the tar.gz? I have read that if you write the shell script on a windows machine you can have issues once you move it to the Linux machine, so I am writing the shell script on the Linux machine using gedit. I am just not sure what to do next.
So far in my script I have,
#!/bin/bash
java -jar myProgram.jar
I am going to try and extract the tar.gz file to the usr/bin directory and see if it runs.
Any suggestions or help would be greatly appreciated.
Thanks in advance,
Ray
Your question is quite "broad" :). I hope you find the following useful.
Do not extract the files to /usr/bin. See e.g. http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard on where and where not to put files on a *nix system.
Extract the jar's to e.g. /opt/yourProgram/*.
The shell script should be inside there too. Make sure its executable (i.e chmod 755 script.sh)
In your shell script add cd /opt/yourProgram to have the proper working directory for your program before you invoke java.
If you want this program to be started easily by everyone create a symbolic link in /usr/bin or better in /usr/local/bin pointing to your script. Do this as last step after everything else is working.
In your shell script you'll have to add the other jars to the classpath e.g.
java -cp lib/some.jar:lib/other.jar -jar myProgram.jar
or
java -cp lib/some.jar:lib/other.jar:myProgram.jar com.acme.ClassContainingMain
Recommended practice: Add set -e at the very beginning of your script
As you already mentioned it's considered harmful to edit a shell script using a windows editor. The reason is that the windows editor will encode line-breaks (i.e. you hit the Return key) differently. This will make bash puke :)
Im not too clear of what you are looking for.
The script that you have written should work absolutely fine if you have placed your script and myprogram.jar at the same level.
And also im not sure how your myprogram.jar is referring the dependent libraries. So can't comment on them. Best bet will be to place your script and all jars together and try running the script.

How to check if python is installed in windows from java?

How can I check from inside a java program if python is installed in windows?
Python does not add its path to the system Path and no assumption is to be made about the probable path of installation(i.e it can be installed anywhere).
Use the Java Runtime to exec the following command "python --version".
If it works, you have Python, and the standard output is the version number.
If it doesn't work, you don't have Python.
Most Python installers add keys to the Windows registry. Here's an article about how to add that information, you can use it to see how to read the information.
Have you tried querying the registry to check if it is installed? It is stored in
software\python\pythoncore
If the user has a (relatively) new version of python, that is installed with the MSI-package, you can use the MsiEnumProducts Function to check if python is installed.
exec(String command)
Executes the specified string command in a separate process.
Check for Python from command
this would work
Process process = Runtime.getRuntime().exec("cmd /c
C:\Python27\python --version");

Categories

Resources