This question already has answers here:
How to install Java application to my linux system
(2 answers)
Closed 6 years ago.
I'm new to Linux and Ubuntu environment.
I'm calling a jar file like this :
java -jar app.jar /somearg /anotherarg
But I find this ugly and I want to call my jar file like :
MyApp /somearg /anotherarg
So I think I have to set a environment variable like MyApp = java -jar app.jar.
But I don't know how can I do it.If anyone can help me I'll be very pleased.Thanks.
Using
java -jar app.jar /somearg /anotherarg
instead of
myapp /somearg /anotherarg
saves you only 2 words, so not that much. Anyway if you run your application frequently it is a good idea to provide an alias. If you are using bash shell (echo $SHELL shows something like /bin/bash) then here's the command you can paste in your terminal:
alias myapp="java -jar /path/to/your/app/app.jar"
After that you can use
myapp /somearg /anotherarg
It is important to provide the whole path to your app.jar file if you run it from different locations. Also if you want that your alias is permament (i.e. it is always after you log in) just add the same line at the end of ~/.bashrc file, using for example:
pico ~/.bashrc
PS. You don't need slash symbol to provide parameters. In unix systems it is more common to use "-" to set argument options.
PSS. Unix systems are case sensitive and it is typical that command line programs are all written in lowercase.
Related
I have files which are created via clojure
frontendapp.jar
backendapp.jar
and take an environment variable, APP_PORT
What I want to understand is how do I pass variables to the jar files so that they run with the variable APP_PORT?
My assumption is something like this
java -jar frontenapp.jar APP_PORT=8080
Am I correct in my assumption?
Apologies if the question is bad, it's my first time deploying a clojure application
Assuming a unixy shell like bash, you can do
$ APP_PORT=8080 java -jar frontendapp.jar
or perhaps more commonly:
$ export APP_PORT=8080
$ java -jar frontendapp.jar
In the first case, APP_PORT will only be set in the environment given to the java process. In the second case, with export, the variable will be set in the environments of all processes subsequently started from that shell.
This question already has answers here:
How to invoke a Linux shell command from Java
(3 answers)
Closed 6 years ago.
I'm using RedHat Linux bash command prompt. My requirement is to run a command using following code snippet where command could be anything which is allowd on command prompt eg: javac etc.
Process p = Runtime.getRuntime().exec(command);
Now i have set my commands bin directory in PATH variable by editing ~/.bash_profile file
export PATH=$PATH:<my commands bin directory>
it runs perfectly when i manually run the command by opening a new command prompt, but when i'm trying to run it using process command it fails as the PATH variable does not have my commands bin directory.
It seems when "Runtime.getRuntime().exec(command);" invokes a new bash shell it does not include or read ~/.bash_profile file.
I have also verified that the user is same when running manually and using java code.
Could anyone point out whats issue here?
Runtime.getRuntime().exec() doesn't search PATH by default. You will either need to find a different method that does this for you or implement it yourself by loading PATH, possibly parsing it, and then iterating through it to find the executable.
I am trying to run a Java program by using jsvc.
I have installed it by
sudo apt-get install jsvc.
To find out a solution, I tried to read the Apache documentation about it (at https://commons.apache.org/proper/commons-daemon/jsvc.html). But this command:
./jsvc -cp commons-daemon.jar:my.jar MyClass
and this other:
./jsvc -cp my.jar MyClass
did not work (of course, I replace the terms by the name of my class etc.).
It gives me the error:
bash: ./jsvc: no such file or directory of this type
So I use jsvc without "./". And I saw here: How to start tomcat with jsvc? that I should use /usr/bin/jsvc
But an other problem is when I use
/usr/bin/jsvc -cp path/to/my/.jar path/to/my/class
nothing happens.
I try the link: How to convert a java program to daemon with jsvc?. But there is something I don’t understand: for the "CLASS =", have I to put a .Main file ? And do I have to put the extension name of the file (for the class and the .jar) ?
I decided to put the .java file which contains my main class (once I putted the .jar, then I didn’t). Then I copied the code, and when I write "esac" and pressed the enter key in the Ubuntu console, the console closed up, and then…nothing.
Has someone already encountered this ?
Are you sure your java installation is in /usr/java?
Beside this, in the second command there's the directory missing. You should do something like that:
export JAVA_HOME=path/to/java/home
./configure
If you don't know where your java installation is located, try this if you are on a mac/*nix, or this if you have windows.
I have created a exe file from jar via converter tools. Jar file was executing fine when I tried to run via unix by passing input parameters eg: java -jar SSS_Infinite.jar test.in 2
However after converting to exe I tried to run by passing input parameters via Unix but its not working and simply returns to the next line. I tried the below command in Unix cmd. Is there any other alternative to make it trigger ?
SSS_Infinite.exe test1.in 2
I assume you created executable for Windows platform, it will not work on *nix systems.
The simplest option will be to build little script that will accept parameters and pass them to java -jar, something like that:
#!/bin/bash
java -jar SSS_Infinite.jar $1 $2
where $1 and $2 are script arguments, see explanation here.
after you create that script and save it as say SSS_Infinite.sh, change its permissions:
chmod +x SSS_Infinite.sh
Then you'll be able to execute it like that:
./SSS_Infinite.sh test1.in 2
This question already has answers here:
How to call java from C++
(4 answers)
Closed 8 years ago.
I wrote a code in java that adjust the system sound volume based on value supplied as command line argument. I would like to execute this code from my C++ application. What is the best way to do this...
Do I need to install the jdk on the deployment machine?
You can bundle a runtime with your software and then call this from within your program, however, the better solution would be to perform the function that the Java app does natively within your C++ application.
You can run your Java application from C++ as you can run any other executable.
Run "java.exe" with your Java class name as a parameter, as you would do normally when executing Java applications from the command line:
java.exe MyClass myParamToJavaClass
Alternatively you can pack your class in an executable jar archive, in which case your command to run your Java application would look something like this
java.exe -jar MyJarr.jar myParamToJavaClass
Substitute the ".exe" by something else if you aren't running under Windows.
In the examples above i assume that "java.exe" is in a directory defined in your "path" environment variable, as it normally is.
You do not need to install JDK but a JRE needs to be present on the deployment machine.