Compiling and Executing a Java File With a Shell Script
I'm trying to use a shell script to compile and execute a java file. The java classes are using sockets, so there is a client.java file and a server.java file, each with their own shell script. I also want to handle the command line arguments within the shell script, not the java classes. The server script should take a port number and try to start a server on that port, if its unavailable it should exit with a message. The client script should take a host name and a port number and do the same.
I've tried a few variations of the below code with no success. Any tips would be really appreciated, thanks!
I'm trying to use a shell script to compile and execute a java file. The java classes are using sockets, so there is a client.java file and a server.java file, each with their own shell script. I also want to handle the command line arguments within the shell script, not the java classes. ...
I think you will have to handle the arguments in both - the shell script and the Java program.
- You pass the arguments from the shell script to the Java class.
- Your Java class accepts and uses those arguments to open the socket, talk to the client/server etc.
Quote:
Originally Posted by britty4
...
...
That's incorrect.
Assuming you have the Java program: "Server.java" that has all the Java code to accept, parse and use the (port number) argument, you will have to do the following (note the commands in red):
(a) Compile the Java program file "Server.java" into a Java class first. Use this command:
The "javac" command, if successful, will create a new file called "Server.class" in the same directory as the "Server.java" file.
(b) Run the Java class file "Server.class", passing the argument to it. Use the following command:
Do the same thing for the Java client program, say, "Client.java".
From a Bash shell script, it would look like this:
You then invoke your Bash script without any arguments because the host and port values are hard coded.
Otherwise, if you want to pass those values to the Bash script, which will, in turn, pass them to the Java classes, then:
And you then invoke your Bash script like so, assuming you have given execute privilege on the script to yourself:
==========
Sorry, I didn't realize you mentioned you had a shell script for each of the Java programs.
If you understood the concepts in the post above, then you shouldn't have any problem using them in your two scripts.
If you get stuck, then post your question with a code snippet and the error you encounter.
Last edited by durden_tyler; 04-10-2017 at 11:53 AM..
Reason: The command "java Server.class 4444" is incorrect. The ".class" extension should not be specified.
I have a .jar placed in my Unix directory. The .jar creates a .csv file .I want to execute the .jar and place the output file in a target Unix directory.
The Unix Script is as follows. The issue that i am facing is that the file is not being placed in the REPORTDIR=/cdunix/IQNavigator/wrk instead... (4 Replies)
Hi Team,
Can you pls advise in java how can we call a shell script, actly I have a shell script at the following location
/opt/dd/ajh.sh
now I can execute this script by opening the session through putty by entering the servers details and password and then navigating to the following... (2 Replies)
I need to write something that will read and execute all the files(Mainly executable scripts) inside one or more folders; in other words, a continuous chain with a break when finished. I'm new to shell and need syntax help. I'm on Ubuntu 12.10-Gnome btw.
Here are some main highlights I think... (2 Replies)
Hi,
I have an shell script program in a remote linux machine which will do some specific monitoring functionality. Also, have some C executables in that machine.
From a windows machine, I want to run the shell script program (If possible using java).
I tried with SSH for this. but, in... (1 Reply)
I have the following lines of script to run sequentially(i.e one after the other as arranged). how can I compile it to one shell script of the form DATABASE.sh
awk '$2~/eaw/{BSC=$3}{print BSC,$0}' RXMOP.log | grep GSM | awk '{print $1,$2,$3,$5}'>TX.data
sed 's/RXOTX/RXORX/g' TX.data>RX.data
sed... (4 Replies)
I want to compile a shell script so that anyone can run it on any linux platform without being able to view its content. Is there any way to do this?
Thanks in advance
---------- Post updated at 12:00 PM ---------- Previous update was at 11:35 AM ----------
shc creates a stripped binary ... (2 Replies)
Hi all,
I am getting : No such file or directory while executing a shell script. But i have that corresponding file in the corresponding path. It also have executable rights. Please help me out in this
Thanks in advance.
Ananthi.U (7 Replies)
Hi ALL,
I would like to execute one SQL query(ORACLE) in UNIX shell script. For this I used sqlplus in script and tested locally. It worked fine. But my requiremnt is to execute the script from Java. In this case the UNIX part is working but sqlplus is not returning anything
The JAVA code used... (0 Replies)
Hi
How can i dynamically read files names from a list file and execute them from a single shell script.
Please help its urgent
Thanks in Advance (4 Replies)