Strange error calling java program from python via command line - java

As part of my python-based program, I have to call a Java program - solved that perfectly with os.system(). It worked well, until I moved the entire thing from one directory to the other. I changed all the filepaths in the python code, and the java program - as best I can tell, I don't really understand java - relies on relative file paths, which weren't changed during the move. But now, the java program won't open. The command line appears then vanishes almost instantly, so I know that os.system() is working. It must be to do with the filepath I'm using in os.system, as when I change that back to the original filepath it works perfectly fine again. Code below:
os.system("java -jar C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
Where fileNames is a variable thingie passed to the java program as an argument, which I'm fairly sure isn't the problem. If I call the python program directly from cmd.exe, then it gives me back the error message "Unable to access Jarfile C:\Documents". I thought this might have to do with the spaces in the filepath, so I put underscores in:
os.system("java -jar C:\\Documents_and_Settings\\enginx_mgr.ISIS\\My_Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
And it gave me the same "Unable to access Jarfile" message, but this time with the full filepath. Trying os.path.exists() on the filepath returns true, so python knows its a real filepath; I guess it must be the command line disagreeing with it, but I don't know why. Any ideas?
Edit: Original filepath, if its of interest, was C:\Inetpub\ftproot\JPivSource\jpivc.jar
Edit 2: Its almost certainly not the filepath, going by the answers below and the fact that none of them work (and that the original filepath works). Checked the security options out of a hunch, and I have full control over the .jar file, as does the system, so its not that it can't access it for security reasons. Still at square zero.

Not a direct answer but ...
I think it is better to call .bat file instead of direct call to java with many command line option. This way you will not need to change Python program to add some other options (like -Xms2048m or -Dfile.encoding=utf8).
Such .bat file is also much easier to debug.

Your problem looks to be caused because of a typo somewhere. This should fix it:
Open Windows Explorer
right click on the file
click "Properties"
copy the location
paste that location into your script, with directories escaped.

You have to put quotes around your path.
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)

I'm sorry for offering up another file path solution, but this wasn't mentioned, and it seems likely that the changing of paths would be causing the issue. So, if you wouldn't mind humouring me?
os.system("java -jar C:\\Documents\ and\ Settings\\enginx_mgr.ISIS\\My\ Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
All I've done differently, is escape the spaces with a backslash.
You said that os.path.exists is returning True, and that's fine, but you're trying to execute a command line program by passing it a number of arguments. The program reading the arguments will interpret the string as several strings because of the spaces.
You could also try altering the quotes that you are using:
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)
That file path you're using does look quite strange.

Related

Why does my batch file give an error if it has spaces in path?

I am trying to run a .jar file using a .bat file. But my Batch file gives an error when I have spaces in my path.
For e.g.
#echo off
java -jar %~dp0\..\new\myFile.jar %*
This are the contents of my batch file. I know that %~dp0 will give the current drive letter and path to my batch file and then \..\ will go back to it's parent directory and then it will open the new directory and run the jar file from it.
But the problem here is if my path contains a space in it the batch file gives an error:
Error: Unable to access jarfile C:\Users\Vatsal
Assuming my path for .jar file is:
C:\Users\Vatsal Sura\myFolder\new\myFile.jar
and path for my .bat file is:
C:\Users\Vatsal Sura\myFolder\batch\batchFile.bat
Now if I move myFolder directly in C:\, which doesn't contain any spaces in it, then it won't give any error and the .jar file would run as expected.
I want to know why this is happening and what should I do to make spaces count in my path?
Put the offending part in double quotes: java -jar "%~dp0\..\new\myFile.jar" %*
If the expansion is C:\Users\Vatsal Sura\myFolder\new\myFile.jar, then without quotes, -jar is only seeing up to the first space as it's argument (i.e. -jar C:\Users\Vatsal) and the remainder (Sura\myFolder\new\myFile.jar) is a completely different argument not related to the -jar
You need to make the shell/cmd see all of that as one argument. Double quotes will still allow any expansion to happen but everything within the quotes will be seen as a single item (the quotes won't be there when java sees it).
The reason for your problem is simple
Batch Is Simple Too,
Windows needed to create command Line, powershell, blah blah, and with its awesome features. However, Batch only runs on one programmed line of code, yes its confusing lol but when you write the code it is nothing compared to the binary. Batch is not too advanced and therefore does not process spaces when its in the "heart" of the command, like it i do this it literally has the same error however batch can tell the difference:
ec ho
(supposed to be echo)
anyways, a simple solution to your problem can be changing the encoding that your batch file is working on or just simply taking out the spaces on you directory.
Plus, don't use
%~dp0\
(it suKs)
Instead, to simplify this just do
#echo off
java -jar %CD%\new\myFile.jar %*
you don't have to even set %CD% to be anything it just automatically used the current directory.
*even with all this, your initial problem will not be solved. It may if you change the encoding (use notepad++ for that) but i hope you found this answer at least a little helpful to explain how this is working.

gvim/vim: launching from within Java program with parameters

I'm calling vim/gvim from within a Java program to convert a source code containing file into html, as follows: "gvim -c \"set syntax=java\" -c \"TOhtml\" -c \"wq\" -c \"q\" -c \"q\" " + Konst.FPATH + "tmp.txt"
However, for some reason gvim thinks "syntax=java" is a name of a file, and ends up spitting out tons of messages ".syntax=java.swp" found, and more importantly does not apply syntax highlight after all. How to fix this?
EDIT: For some reason, "set syntax=java" is misinterpreted, and gvim opens a file syntax=java" -- exactly this, with a double-quote at the end.
Executing an external program from Java is generally not a good idea, especially if this code run on a web server. It might lead to serious security issues.
What are you trying to do? If your goal is to highlight some Java code, just use a code highlighting library.
I don't know any Java highlighting library, but you can find some good Javascript libs (highlightjs, prismjs, code-prettify, etc.).
I solved this with "gvim -s script.vim" command, where in script.vim I put (!) ":set syntax=java" etc -- yes, starting with ":".

Number and type of slashes in path in command prompt

This is kind of a ridiculous question but I am stucked with it and I need some help.
I am trying to install an open source software in my windows 8 system using command prompt. Everything is set correctly but when I type the final java command, to run the software, I get a series of exceptions.
I am sure that the problem is related with some slashes in the path. I know that the type and number of slashes is important and sometimes java requires this.
So I am not sure if I can get an answer about such a specific question here but I give it a try. This is the path I run in one line:
C:\Kuwaiba7\service>java -Djava.rmi.server.codebase="file:/C:/Kuwaiba7/service/lib/PersistenceAbstractionAPI.jar file:/C:/Kuwaiba7/Service/lib/PersistenceServiceRemoteInterfaces.jar" -jar PersistenceService.jar
I double/tripled checked the paths and tried different variations with the slashes but nothing gives me the expected result.
I get the following exceptions:
java.rmi.serverException
java.rmi.unmarshalException
java.lang.classNotFoundException
This is the format of the path they suggest in their manual but doesn't seem to work either:
Thanks
Dimitris
I found the solution and I post the answer here. Afterall the slashes were correct the way I had them. Although sometimes in Java the number and the type of the slashes counts that wasn't the case here.
What I did before was that I was starting the rmiregistry and then running the PersistenceService. That didn't work.
Then I tried to set the Classpath (from the directory where the PersistenceService.jar is located in my PC). After start the rmiregistry.exe (from the Java directory) and finally run the persistence service as shown above.
That made it work!

Command-line options working in windows, but not Linux

I have a .bat file on Windows/shell script on Linux that starts a large Java application from the command line. It configures the classpath, environment variables, etc.
At one point, it uses RMID to configure a bunch of services which will run in their own JVMs. The problem is that it won't allow me to specify multiple JARs for the codebase property on Linux. It's allowing me to do so on Windows just fine, but I think my syntax/styling must be wrong for the .sh script and am hoping a more experienced Linux user could have some tip. On Windows, the working line looks like this:
SET RMID_OPTIONS=%RMID_VM%
-J-DINSTALL_DIR=%CONFIG_PATH%
-C-DINSTALL_DIR=%CONFIG_PATH%
-J-DINSTALL_DIR_LOCAL=%HOME_DIR%
-C-DINSTALL_DIR_LOCAL=%HOME_DIR%
-J-Djava.security.policy=%PL_HOME%\windows\system.policy
-C-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
-J-Djava.rmi.server.codebase=
"file:/%HOME_DIR%\jar1.jar file:/%HOME_DIR%\jar2.jar"
// more stuff here
The only important lines are the ones setting the rmi.server.codebase property. The above works 100% fine, however, when trying to set multiple JARs in the codebase in Linux, it causes a general failure and the whole RMID command is not executed. My shell script looks like the following:
export RMID_OPTIONS="${RMID_VM}
-J-DINSTALL_DIR=${CONFIG_PATH}
-C-DINSTALL_DIR=${CONFIG_PATH}
-J-DINSTALL_DIR_LOCAL=${HOME_DIR}
-C-DINSTALL_DIR_LOCAL=${HOME_DIR}
-J-Djava.security.policy=${PL_HOME}/linux/system.policy
-C-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
-J-Djava.rmi.server.codebase=
""file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar""
// more stuff here
"
The shell script itself works perfectly fine if only one JAR is specified, but any more and I get a general failure. Any suggestions on what I'm doing wrong? I'm open to try new things to fix this as all my attempts so far have been fruitless.
Under Linux, escaping quotes is done differently. You are attempting to use the Windows specific syntax, which will result in the jar files being passed as separate arguments, instead of a single one, as it should be.
Instead of "" to produce a quote inside quotes, you have to use \" in Linux:
export RMID_OPTIONS="... -C-Djava.rmi.server.codebase=\"file:/${HOME_DIR}/jar1.jar file:/${PL_HOME_LOCAL}/jar2.jar\" ..."
Aside from that, I'm not sure that the file:/ syntax is correct. It's probably either file:// or the absolute file path without anything preceding it, but you'll have to try it out.
You're doing this wrong. You don't need to start rmid with arguments and system properties at all. All that stuff should be specified when you register the ActivationGroup(s) you're going to use, in your activation setup program. That in turn means that all command-line problems should just disappear.

how to leave cmd open after batch file executes

I am current'y taking a Java course and because of security reasons it won't let us set environment variables for the Java compiler. What we have to do all the time is open cmd and then put
set path="path_to_java"
This gets really annoying because when we close the command line it loses the path. I was able to create part of the .bat file but when I execute it, then it closes instantly. I know you are able to put PAUSE but then it won't let us insert any Java code.
Is there any way to create a .bat file and fix this problem so when I double click it creates path variable and it lets us compile Java code?
I currently only have this
SET PATH "path_to_compiler"
CLS
There are several things you could do. One way would be to create your batch file somewhere (e.g. in your profile folder) and make it automatically execute whenever you start a command prompt:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "%USERPROFILE%\init.cmd"
However, since you're not allowed to set persistent environment variables, this may be prohibited as well. In that case you could create a shortcut to your batch script on the desktop, then open its properties and change the target to something like this:
%COMSPEC% /k C:\path\to\your.cmd
%COMSPEC% is the CMD executable, and the option /k prevents it from automatically closing after the script finished.
As a side note, you may want to include the current %PATH% with the path to the compiler, otherwise stuff may stop working (e.g. because some command line utilities can't be found anymore):
set PATH=%PATH%;C:\javac\folder
I haven't checked for duplicates, but I'm pretty sure I've seen a question about this recently. Anyhow, you aroused my curiosity as to how to do this, so I experimented, and fortunately the first thing I tried worked.
So, basically what you should have is...
#echo off
::Add your code under here
set path=path_to_compiler
cls
call cmd
One thing I must ask, is it absolutely necessary to make the variable called "path" instead of something else? I'm asking this because path is an important variable that the interpreter uses to do things.
Anyway, this basically just runs your code then opens up cmd.exe within the current window.

Categories