so, I've been doing some searching, and i can find somethings on how to run an external application, but i cant get them to work! I've been working on this for a while, and its really annoying.
what i want to do is run a .jar file in the directory
C:\Program Files\AVTECH\NPS\Files\bin\NPS.jar
and I've tried a bunch of different things with the code
Process p = Runtime.getRuntime().exec("dir goes here");.
also
Process p = Runtime.getRuntime().exec("C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar");.
if i'm correct, it uses command prompt to do this? or at least the MS-DOS language. i did some of that kind of thing a few years ago, but i don't remember how one would do this... I've never worked with this kind of thing in java before...
could someone help please? thanks in advance.
Runtime.exec() is working just like if you were typing a command.
Launching a jar file is not working : you have to invoke
java -jar /path/to/my/jar
Check Oracle's documentation on how to execute a jar file.
The actual command should be java -jar C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar. I mean -- if the jar file is indeed executable, this doesn't mean it will run, by just trying to invoke it. You need to tell Java to run it as shown above.
In addition, MS-DOS is not a language -- it stands for Microsoft Disk Operating System. Nowadays, you have this as a Command-line Prompt (Shell) built into Windows.
You need to run the command as a call to the executable and a set of arguments. Check this version of Runtime.exec(String[] cmdarray). If need be, there's also a version of Runtime.exec() that takes a base directory in which to start the executable.
Related
completely new to this so apologise for the question.
I have current been asked to deploy a jar on a linux server at work. I have sucessfully logged into ther server using ssh and transferred my jar on to it. However when I run any java command it says command not found.
I ran
locate java
and
locate jar
which both return, so im guessing java is installed? What do i need to do?
Thanks in advance
It sounds like your path has not been set up to fit your Java needs. You can do either of two things:
Type out the full path of where java is located, and use this full path instead of "java".
Edit your PATH to include java. Here's some information on how this is done: http://www.java.com/en/download/help/path.xml
I think you have to check to make sure that java is installed and the path for it is set too.
Whe you type locate jar it's most likely going to return any other jar including the one you have just added. To know if java is installed, you should instead use the following command java, if the result is something like the program cannot be found... then it means there is not java. You could also use java -version which will also tell you which version is installed. If it's for running purposes then you will be good with that.
First try the command which java, it would show you if you have java installed if not download it and install,run it. Then you can run the jar file as per you requirement. This link shows you how to install and configure java.
http://www.java.com/en/download/help/linux_install.xml
Thanks & Regards,
Alok Thaker
I already have a working code to download certain file from my server. This file is in an executable (a patch installer). I would like to know if it is possible to launch the executable after my java application has downloaded in its own directory. If it is, may I pleas know how?
Also, I know I am asking for too much here, but is it possible to then delete this installer from the computer after it has installed the patch?---I've been working on this but have stumbled upon a lot of nothing...
Thanks a lot in advance!
Look at the java docs for the class RunTime, this should allow you to run an executable providing you have permissions to do so.
Runtime.getRuntime().exec("command") will execute the input as if you entered it in the command line. So, if you know the path of the exe file (which you should, since you downloaded it), you can launch it like this:
Runtime.getRuntime().exec(executablepath);
Normally you can delete a file by using File.delete(), but in this case the command will still be running if you add delete() right after exec() and it won't be able to delete. Hope that helps!
I want to run a jar file in both unix and windows without have to call it directly with java like:
java -jar myjar.jar parameters
i want :
myjar.jar parameters
I've been reading allready -
Running a JAR file without directly calling `java`
Which seems like a very nice hack for unix .
Howerver , this wont work in windows.
I'm looking for a uniform solution that will work both on unix and windows , but I'm not sure there is such.
The solution has to be only once , and it has to include changes related to the jar only ,and not the operation systems - because this is a file to I'm suppling to a client.
What you are asking can't be done: Windows will load executable files only in the PE/COFF format used in .exe in .dll files.
What you can do instead is supply the users a "wrapper" program that starts the actual Java program. You could create the wrapper in C, which has several benefits: you can set an icon on the executable and associate the program with file types in the Windows Explorer. Batch files are a popular alternative; they are easier to create.
You can provide a script that starts your application for both Unix (.sh) and Windows (.bat). This seems to be the preferred approach for many companies. An example would be JBoss Server where a run.bat is provided for Windows and a run.sh is for Unix. These scripts set the appropriate environment variables, classpath, etc and then call java.
You can write your own bash and batch/powershell scripts. As for windows, you can try Launch4J. It should be easier than writing elaborate scripts from scratch.
Be aware that you can only provide wrappers to make the execution simpler (one click). Java has to be run anyway. Either explicitly, by the user, or as part of a script. You can't do without it.
I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?
There is a JVM argument -Duser.dir which can be used to set working directory for JVM.
If it all possible I would rather use a script to run the java application and set the directory in the script:
#!/bin/sh
cd <your dir>
java <some arguments>
The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.
If you want to change the current directory, you'll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use SetCurrentDirectory
I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.
I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.
In this situation, the Working Directory will be set to "JarLearning" rather than "2ndTry".
How to implement the Linux 'top command' style UI using JAVA?
And How to run jar in server side itself, and i can see the state of the program after i log in SSH.
For your second question:
java -jar jarfile.jar
Will run a jar file that is written to be run at the command line. Anything this program prints to standard out will be displayed for you to see.
This entry talks a bit about building a jar file that can be run in this way:
http://en.wikipedia.org/wiki/JAR_%28file_format%29
You could give this a try. I'm assuming that you're looking for an ASCII terminal UI toolkit... which I'd recommend, because terminal codes are rather painful to write by hand.
You might be looking for a Java Curses Framework like this:
http://javacurses.sourceforge.net or CHARVA