Compiling and Executing a Java File With a Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compiling and Executing a Java File With a Shell Script
# 1  
Old 03-28-2017
Question 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!

Code:
#!/bin/bash

javac server.java $@

hostName = $1
portNumber = $2

# 2  
Old 03-31-2017
Quote:
Originally Posted by britty4
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
...
...
Code:
#!/bin/bash
javac server.java $@
hostName = $1
portNumber = $2

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:
Code:
javac Server.java

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:

Code:
java Server 4444

Do the same thing for the Java client program, say, "Client.java".
From a Bash shell script, it would look like this:

Code:
#!/bin/bash
host_name="myserver.example.com"
port_number=4444
  
# First, compile the Java programs into Java classes
javac Server.java
javac Client.java
  
# Now pass the arguments to the Java classes
java Server ${port_number}
java Client ${host_name} ${port_number}

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:

Code:
#!/bin/bash
host_name=$1
port_number=$2
  
# First, compile the Java programs into Java classes
javac Server.java
javac Client.java
  
# Now pass the arguments to the Java classes
java Server ${port_number}
java Client ${host_name} ${port_number}

And you then invoke your Bash script like so, assuming you have given execute privilege on the script to yourself:

Code:
./client_server.sh myserver.example.com 4444

==========
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing java .jar from UNIX script

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)
Discussion started by: pankajkargeti12
4 Replies

2. Shell Programming and Scripting

Executing the shell script through java program

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)
Discussion started by: punpun777777
2 Replies

3. Shell Programming and Scripting

Creating a Continuous File Reading-Executing Shell Script

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)
Discussion started by: linuxlololol
2 Replies

4. Shell Programming and Scripting

Running remote system shell script and c binary file from windows machine using java

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)
Discussion started by: ram.sj
1 Replies

5. Shell Programming and Scripting

Compiling to shell script

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)
Discussion started by: aydj
4 Replies

6. Shell Programming and Scripting

Compiling Shell script

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)
Discussion started by: proactiveaditya
2 Replies

7. Shell Programming and Scripting

getting : No such file or directory while executing a shell script

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)
Discussion started by: ananthi_ku
7 Replies

8. UNIX for Advanced & Expert Users

Executing SQLPLUS in UNIX Script from JAVA

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)
Discussion started by: anooptech
0 Replies

9. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

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)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question