Active6 years, 1 month ago
I'm trying to run a client-server program in java on Netbeans.
This document describes how to create a simple web application that connects to a MySQL database server. It also covers some basic ideas and technologies in web development, such as JavaServer Pages (JSP), JavaServer Pages Standard Tag Library (JSTL), the Java Database Connectivity (JDBC) API, and two-tier, client-server architecture. This tutorial is designed for beginners who have a basic. Though I prefer SQL Server Management Studio to access and work with. Of Eclipse IDE, or whatever IDE they use e.g. Netbeans or IntelliJ,.
Here's the code for the server:
Here's the code for the client:
So when I want to simulate the client and the server running together I first run the server.Once the server is running I need to run the client.
However when I try to run the server file, I get the following error:
run:Exception in thread 'main' java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingServer.main(GreetingServer.java:48)Java Result: 1BUILD SUCCESSFUL (total time: 0 seconds)
Similarly, when I try to run the client program, I get the following error:
run:Exception in thread 'main' java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingClient.main(GreetingClient.java:10)Java Result: 1BUILD SUCCESSFUL (total time: 0 seconds)
Why is this happening? Please help.
UPDATE:
There is no error on the server side now. But I am still getting the following error on the client side:
run:Exception in thread 'main' java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingClient.main(GreetingClient.java:10)Java Result: 1BUILD SUCCESSFUL (total time: 0 seconds)
Anon
AnonAnon32544 gold badges1919 silver badges4141 bronze badges
2 Answers
Your code supposes that you pass the port you want to use as an argument so :
- Either you run your application with the line Patryk RoszczyniaĆa posted
- If you don't want to use it, just delete the lines :and replace them by hardcoded values like :
Update :
I just tested your code, in fact, the serverName is the address you use to connect to the server, so replace it by
'localhost'
if you are running both client and server on the same machine.Tested code :
Here is the updated code I tested and it should work perfectly if you run both on the same machine.
Client :
Server :
PadrusPadrus1,53411 gold badge2020 silver badges3434 bronze badges
To run
SocketServer
you need to pass port on which server will work. For example:The problem is that you want to pass port by java argument and you aren't doing that. First of all before use of any variable you should first validate it.
Your program requires params but I think that you probably run your program without params.You should run program in this way:
java -jar app.jar 1234
But you do it in this way: java -jar app.jar
In this article you will find how to pass parameters to java program using netbeans http://netbeanside61.blogspot.com/2009/02/using-command-line-arguments-in.html.
If you are new in java you should first read this tutorial http://docs.oracle.com/javase/tutorial/networking/sockets/. It's very helpful.
pepuchpepuch4,21544 gold badges4141 silver badges7272 bronze badges