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");
Related
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.
How to select a directory and fire a command via Terminal using java code in Ubuntu.
For example i want to select the directory of tomcat like "cd /home/sree/tomcat/bin" and fire command like "sh shutdown.sh" and "sh startup.sh" for accessing sh files using java coding.
Also need help for the above process in windows operating system.
Please any one give me a solution. Thanks in advance
You could use Apache Commons CLI to create a program that calls the desired commands you want (you would need to create a version for both Ubuntu and Windows). It offers a lot of flexibility and possibility to plugin to the system pipelines.
After that, you package your program as a jar and run it from the directory you need.
Use the following function:
java.lang.Runtime.getRuntime().exec("a-command");
also, this post may help you:
http://blog.art-of-coding.eu/executing-operating-system-commands-from-java/
There are two classes for this,
java.lang.Runtime
More details
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
java.lang.ProcessBuilder
More details
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
ProcessBuilder in Java
I have used process builder, for that you need to create an instance of ProcessBuilder first and call the start method on it, pass the command that you want to execute as constructor arguement.
ProcessBuilder pb = new ProcessBuilder("ls");
Process p = pb.start();
p.destroy();
is the following command working under Unix&Linux?
ProcessBuilder prcbdoc = new ProcessBuilder("cmd","/C","start", "Documentation.doc");
prcbdoc.directory(new File(currentDir+"/docs/"));
prcbdoc.start();
I'm not sure because of the "cmd" "/c"
€:
What would be an Linux CentOS equivalent command?
No, it doesn't work. From Java documentation:
a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. For example, it is common for each conceptual argument to be an element in this list, but there are operating systems where programs are expected to tokenize command line strings themselves - on such a system a Java implementation might require commands to contain exactly two elements.
To open a document in a portable manner using AWT:
if (Desktop.isDesktopSupported())
Desktop.getDesktop().open(documentPath);
No, this would not work on Linux (or any other Unix, or on the Mac) due to the "cmd /c" and the "start". On the Mac, you'd say "open Documentation.doc".The various Linux desktops have their own versions of the start/open command: gnome-open for the Gnome desktop, and the FreeDesktop semi-standard xdg-open are some possibilities.
No. cmd and /c are relevant only for windows.
I have created a java application and packed it into a jar file on a Windows platform. Now I wants to deploy that jar file on Debian Linux.
Will the same jar file work for Debian Linux?
Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
i.e.
java -jvm "MyJar.jar"
Will the above command work for Debian Linux?
Generally, it should. However this depends on a few conditions:
If you use native code (JNI) you must make sure that the native library is available for the target platform
You must make sure you have no paths hardcoded which are Windows specific (in fact you should even watch out for special characters like the Path seperator : vs. ;)
You cannot use Runtime specific code
Yes.
Jar files are portable across platforms, and the syntax of the jar command is the same on both Linux and Windows.
EDIT: You should use the latest version of Sun Java unless there is a very good reason not to. Installation instructions: http://wiki.debian.org/Java/Sun
1. Will the same jar file work for Debian Linux?
Yes. Hence the nature of Java (portable code)
2. Is the command, used in windows for executing a jar file from the command prompt, same for Debain Linux?
java -jar "MyJar.jar"
yes, the main idea of java is that it (should) run on different operating systems, as long as a java runtime is installed.
though i have never heard of the -jvm flag.
if you want to start a jar file you should use the -jar flag.
java -jar "MyJar.jar"
you can also read up on the Write once run anywhere principle.
I do my development on a mac but run in linux and windows environments without any problem. Key is not to use JNI, As everyone else have mentioned I would use java -jar "MyJar.jar"
Almost. Use:
java -jar "MyJar.jar"
And of course you shouldn't have used anything such JNI or runtime stuff
Yes, although you might want to do, in Linux:
java -jar YourJar.jar
Instead of:
java -jvm YourJar.jar
I want to start nutch from Java. How can I start cygwin from a Java program?
First of all you have to set the bash.exe to environment variable so this line will start bash.
Runtime rt= Runtime().getRuntime().execute("bash");
Rather than use Java to start Cygwin in order to invoke nutch you should probably look into integrating Nutch directly with your Java app. There's some documentation here:
"While the Nutch web app is a great way to get started with search, most projects using Nutch require the search function to be more tightly integrated with their application. There are various ways to achieve this, depending on the application. The two ways we'll look at here are using the Nutch API and using the OpenSearch API."
If you are trying to run a binary that requires the cygwin1.dll (which includes most commands you can execute from the cygwin bash shell) then you can run it by specifying the cygwin\bin directory in the path environment variable like this:
Process p = Runtime.getRuntime().exec(
"C:/path/to/cygwin/binary.exe", new String[] { "PATH=C:\\cygwin\\bin" });
This assumes you installed cygwin in C:\cygwin
You can use any Cygwin program without passing thru Cygwin, just like any regular Windows Console Application. Take a look at the Cygwin\bin\ dir. You may even bring the programs you want to your app dir, as long as you bring a copy of CYGWIN1.DLL as well.
A more interesting, more clever thing to do, would be starting Nutch from Cygwin or using a real Linux/Unix system.