Create a Run file in java - java

Is there a way to create a "Runfile" in java which runs the program just by using a command run, like Makefile and make?
I am using linux and have to type this command very often java -cp ../lib/*:../zookeeper-3.4.6.jar:. WordCount. It would be convenient if there was a easier way!

Make a shell script name run and place your required command.
Now make the run script executable -
chmod u+x run
Now you just execute the shell script from the terminal. If you need further improvement and if you are in ubuntu then you can create a bin directory at your home (~). Then you can place all of your commands in it. Generally your ~/.profile file contains information about the bin directory at your home. Now the bin directory works as your private bin. If your ~/.profile file doesn't contain any information about the bin directory then you can add the following line in your ~/.profile -
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi

Related

How do i fix this .bat file so it can run in macOS?

I have this .bat file created and working perfectly in Windows. When I tried to run this file from macOS terminal, it shows some error.
I've already had JRE installed in my Mac. I also added:
JAVA_HOME=/Library/Java/Home
export JAVA_HOME;
to my .profile file. All the jars needed are also in a folder beside the .bat file.
This is what's inside the .bat file:
shell
"%JAVA_HOME%\bin\java.exe" -cp .;libs/*;api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
I tried deleting the 'java.exe' from code above, but the error still showed up.
I expect to run this .bat file perfectly.
When it comes to setting JAVA_HOME (on macOS) it's better to use
export JAVA_HOME=$(/usr/libexec/java_home)
inside your ~/.profile. You can also pick any version you like by using -v option.
To list all JVM installations, call:
/usr/libexec/java_home -V
to select one of them, use
export JAVA_HOME=$(/usr/libexec/java_home -v version)
then, you can use it like this
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
turning .BAT to .sh
you can also create a wrapper script like this
#!/bin/bash
export JAVA_HOME=$(/usr/libexec/java_home)
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
Make sure to make it executable
chmod +x script.sh
Then, you can call it following way
./script.sh

XLDeploy dar file creation using windows command prompt

Please refer the link:
https://docs.xebialabs.com/xl-deploy/how-to/create-a-deployment-package-using-the-command-line.html
Collect the EAR file and the configuration directory and store them in the directory:
cp /some/path/petclinic-1.0.ear petclinic-package
cp -r /some/path/conf petclinic-package
Now what is cp? Java command? Maven command? Windows command?
'cp' is not recognized as an internal or external command,
operable program or batch file.
cp is the Unix/Linux file copy command. The first line copies a single file. The second line with cp -r copies recursively: it copies the directory tree.
You can achieve the same with the windows "copy" command or by performing these steps manually in the windows explorer.

Shell script to launch Java app won't execute

I am using debian and xterm.
I have created a file 'run.sh' containing the following:
java -cp bin Main
read -n1 -r ip "Press any key to continue..." key
In Properties window I gave it permission to run as program.
Double clicking the file does nothing. Right click 'Execute' does nothing. Open-with UXTerm does nothing.
If I open a terminal in the same directory and type
java -cp bin Main
then it will run, but the shell script file never works.
What am I doing wrong here?
Your shell script file doesn't seem to have a shebang line,
#!/usr/bin/env bash
java -cp bin Main
read -n1 -r ip "Press any key to continue..." key
and make sure it has a execute permissions
chmod a+x <script_file>
You need to add a shebang line at the top of your file: #!/usr/bin/bash. This tells the operating system that the file is in fact a bash executable rather than a normal file. Alternatively, execute the script by entering bash run.sh from the command line.

Cannot find JAVA_HOME in Red Hat Server

I'm trying to find the location where JAVA_HOME env variale is set up and I tried to find in ~/.bash_profile , ~/.bashrc and /etc/profile but could not find the env variable.
But when I run echo $JAVA_HOME its gives out the value /usr/local/java.
Where else would the JAVA_HOME env variable.
BTW its a Red Hat Linux Server.
Try the file /etc/bash.bashrc, sometimes it is also used to initialise bash. If not, then try to find the word JAVA_HOME inside the files in /etc with grep -r BASH_HOME /etc
UPDATE
From man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes
commands from the first one that exists and is readable.
and also:
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist.
So the only file that you haven't had a look at is ~/bash_login.
Probably JAVA_HOME is not in any of these files and what happens is that one of these files call another script, in that case, you'll have to read line by line if one of these scripts is "loading" more scripts from other places
What about:
grep JAVA_HOME /etc/*
and:
grep JAVA_HOME ~/.*
Please also check variables been sourced in all startup scripts in /etc/ and also under home directory,
it will look like . path/filename or source path/filename
EX: . /etc/var
or
source /etc/var
There are few standard locations which environment variables can be set.
/etc/profile
shell scripts under /etc/profile.d
/etc/bashrc
~/.bashrc
~/.bash_profile
Recommended way is to put environment variables in a script under /etc/profile.d. You can grep this directory, if other locations failed.
grep -R "JAVA_HOME" /etc/profile.d/
This post can give more details on the topic.

Script Not Running

I have a script file that I would like to run whenever my computer starts up. What the script file does is run a .jar file that I have on my desktop.
I first created a .jar file called Hello.jar that is located on my desktop. After that I created a script file (.sh) called Script.sh that has the following contents in it.
cd Desktop;java -jar Hello.jar;
Then I followed this answer to run the file on startup. So as it says I first setup a .desktop file by running this command in the terminal.
sudo cd Desktop
sudo mv Script.sh /usr/bin
Then I did
sudo cd /usr/share/applications
sudo gedit file.desktop &
Then I wrote the following information in gedit.
[Desktop Entry]
Name=Hello.sh
Exec=/usr/bin/file.sh
Type=Application
Terminal=false
And lastly I created a copy of it in this location.
/etc/xdg/autostart/
I then restarted my computer but nothing happened.
sudo cd doesn't do anything! The cd command only takes effect within the current shell - which immediately exits!
Instead you should do sudo bash to launch a root shell. Then run all your commands within that root shell.
Also, I think you forgot to give your script execute permissions. You can do that by changing mv to install.

Categories