I am a PHP/Mysql programmer trying to learn Java and I'm stuck on how to complile a file with this in it:
public static void main(String[] args)
Here is example code:
import java.net.*;
import java.io.*;
public class GreetingClient
{
public static void main(String [] args)
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out =
new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
When run the try to use the javac command in Windows Command prompt to compile this from a .java file into a .class file to be called in a webpage, I get an error saying:
bad class file: .\String.java
file does not contain class String
Please remove or make sure it appears in the correct subdirectory of the classpath.
public static void main(String[] args) {
If a compile a .java file like this one:
import java.applet.Applet;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
public class applet_test extends Applet {
private InetAddress addr = null;
public void init() {
try {
addr = InetAddress.getLocalHost();
}
catch (UnknownHostException e) {
System.exit(0);
}
}
public InetAddress getLHost() {
return addr;
}
}
I get no errors, the .java file compiles into a .class file and I am able to use the .class file in a webpage just fine.
Not sure what I'm doing wrong.
Thanks guys!
Ok, so now, when I run this command:
C:\ > javac GreetingClient.java
I get this:
GreetingClient.java:9: cannot find symbol
symbol : method parseInt(GreetingClient)
location: class java.lang.Integer
int port = Integer.parseInt(args[1]);
^
GreetingClient.java:14: cannot find symbol
symbol : constructor Socket(GreetingClient,int)
location: class java.net.Socket
Socket client = new Socket(serverName, port);
^
2 errors
Again, I only seem to get errors when running the javac command on a file with this line in it:
public static void main(String[] args)
I know I'm missing something, any help would be appreciated it.
Since your file contains class GreetingClient, it must be named GreetingClient.java. Java requires that the name of the file match the name of the class defined inside it.
In Java, your main class must contains public static void main(String[] args) to initialize it.
If you have more than one class, the other will be just class Class_Name {} and, as Greg Hewgill said: your Java File must be named equal your main class name
Related
I am trying to execute pig scripts from java so that it can connect to the cluster and execute the logic. I am following this link, but how this is going to connect my cluster as i am not mentioning the URL. How can i connect it remotely ?
Code:
import java.io.IOException;
import org.apache.pig.PigServer;
public class idlocal{
public static void main(String[] args) {
try {
PigServer pigServer = new PigServer("local");
runIdQuery(pigServer, "passwd");
}
catch(Exception e) {
}
}
public static void runIdQuery(PigServer pigServer, String inputFile) throws IOException {
pigServer.registerQuery("A = load '" + inputFile + "' using PigStorage(':');");
pigServer.registerQuery("B = foreach A generate $0 as id;");
pigServer.store("B", "id.out");
}
}
I'm trying to connect all my clients to one server. I've done some research and found out that the easiest way to do it is create a new thread for every client that connects to the server. But I am already stuck on the part where a client disconnects and reconnect.
Client
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Test {
private static int port = 40021;
private static String ip = "localhost";
public static void main(String[] args) throws UnknownHostException,
IOException {
String command, temp;
Scanner scanner = new Scanner(System.in);
Socket s = new Socket(ip, port);
while (true) {
Scanner scanneri = new Scanner(s.getInputStream());
System.out.println("Enter any command");
command = scanner.nextLine();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(command);
temp = scanneri.nextLine();
System.out.println(temp);
}
}
}
Server
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) throws IOException {
String command, temp;
ServerSocket s1 = new ServerSocket(40021);
while (true) {
Socket ss = s1.accept();
Scanner sc = new Scanner(ss.getInputStream());
while (sc.hasNextLine()) {
command = sc.nextLine();
temp = command + " this is what you said.";
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
}
}
}
When I connect once it works correctly but as soon as I disconnect the client and try to reconnect (or connect a second client) it does not give an error or anything it just does not function. I am trying to keep it as basic as possible.
The output with one client:
When I try and connect a second client:
I hope someone could help me out. Thanks in advance.
Your server currently handles only 1 client at a time, use threads for each client, Modify your server code like this:-
public static void main(String[] args) throws IOException
{
ServerSocket s1 = new ServerSocket(40021);
while (true)
{
ss = s1.accept();
Thread t = new Thread()
{
public void run()
{
try
{
String command, temp;
Scanner sc = new Scanner(ss.getInputStream());
while (sc.hasNextLine())
{
command = sc.nextLine();
temp = command + " this is what you said.";
PrintStream p = new PrintStream(ss.getOutputStream());
p.println(temp);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
};
t.start();
}
}
So we're fooling around with ServerSockets in class, making a very simple HTTP server that takes a request, does nothing with it, and responds with a 200 OK followed by some HTML content.
I've been trying to figure out this problem for two days, and I haven't been able to get to grips with it, and neither has my teacher. I've come to think it is a problem with closing the server, for some odd reason. I've fixed the problem, but would just like to know why I happened in the first place.
Here are three snippets:
HttpServer.class:
package httpserver;
import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class HttpServer implements Closeable {
public static final int PORT = 80;
public static final int BACKLOG = 1;
public static final String ROOT_CATALOG = "C:/HttpServer/";
private ServerSocket server;
private Socket client;
private Scanner in;
private PrintWriter out;
private String request;
public HttpServer() throws IOException {
server = new ServerSocket(PORT, BACKLOG);
}
public Socket accept() throws IOException {
client = server.accept();
in = new Scanner(client.getInputStream());
out = new PrintWriter(client.getOutputStream());
return client;
}
public void recieve() {
request = in.nextLine();
System.out.println(request);
}
public void respond(final String message) {
out.print(message);
out.flush();
}
#Override
public void close() throws IOException {
if(!server.isClosed()) {
client = null;
server = null;
}
}
}
Main.class solution that works:
package httpserver;
import java.io.IOException;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
HttpServer server = new HttpServer();
Socket client;
while(true) {
client = server.accept();
server.recieve();
server.respond("HTTP/1.0 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "\r\n"
+ "<html><body><b>hello..</b></body></html>");
client.close();
}
}
}
Main.class solution that doesn't work:
package httpserver;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try(HttpServer server = new HttpServer()) {
while (true) {
server.accept();
server.recieve();
server.respond("HTTP/1.0 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "\r\n"
+ "<html><body><b>hello..</b></body></html>");
}
} catch(IOException ex) {
System.out.println("We have a problem: " + ex.getMessage());
}
}
}
I could imagine it has something to do with not closing the client socket after each loop iteration. But even so, it should at least go through once, before bugging up in that case. I really can't see what the problem is supposed to be.
No error messages, nothing...
You do not specify any Content-length when sending the HTTP, so the browser does not know when to stop reading for more data. See How to know when HTTP-server is done sending data for more info.
In the working example you closed the client socket, which tells the browser there is no more data - for your ambitions this might be enough if you don't want the browser to respond.
Trying to compilie this AuctionClientMain.java and this is the error I get and can't figur it out:
AuctionClientMain.java:16: cannot access AuctionClient
bad class file: .\AuctionClient.class
class file contains wrong class: Assignment.AuctionClient
Please remove or make sure it appears in the correct subdirectory of the classpath.
AuctionClient a = new AuctionClient(args[0],args[1],port);
I have included AuctionClientMain.java
import Auction.*;
import java.io.*;
public class AuctionClientMain
{
//Create the client
public static void main (String args[]) throws IOException
{
if(args.length!=3)
{
throw new RuntimeException ("Syntax: java AuctionClient <name> <serverhost> <port>");
}
//Convert port taken in as string to an integer
int port = Integer.parseInt(args[2]);
AuctionClient a = new AuctionClient(args[0],args[1],port);
}
}
And the Auction Client
package Auction;
import java.io.*;
import java.net.*;
public class AuctionClient
{
public AuctionGui gui;
private Socket socket;
private DataInputStream dataIn;
private DataOutputStream dataOut;
//Auction Client constructor String name used as identifier for each client to allow server to pick the winning bidder
public AuctionClient(String name,String server, int port)
{
//Create a new gui
gui = new AuctionGui("Bidomatic 5000");
//Add the key listener to the input field
gui.input.addKeyListener (new EnterListener(this,gui));
//Add the exit listener to the window
gui.addWindowListener(new ExitListener(this));
try
{
//Create a new socket with server name and port number provided
socket = new Socket(server, port);
//Create new data input stream
dataIn = new DataInputStream(socket.getInputStream());
//Create new data outpit stream
dataOut = new DataOutputStream(socket.getOutputStream());
dataOut.writeUTF(name);
while (true)
{
gui.output.append("\n"+dataIn.readUTF());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Send bid to output stream
public void sentBid(String bid)
{
try
{
//Write bid out
dataOut.writeUTF(bid);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void disconnect()
{
try
{
socket.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Your AuctionClientMain class seems to be in the default package. The AuctionClient class is in package Auction. The .class file for AuctionClient needs to be in a subdirectory named Auction relative to AuctionClientMain.
Alternatively, put AuctionClient in the default package or put AuctionClientMain in package Auction.
On a side note, Java conventions are that package names are all lower case. It would be better to use package auction; instead of package Auction;.
Because AuctionClient is in the Auction pacakge, the compiler expects to find the Java source file in a directroy called Auction.
Hey I'm trying to compile the following piece of code to basically read stuff from a file but it refuses to work. it gives me an java.io.FILENOTFOUNDEXCEPTION error at line4. help would be appreciated.
import java.io.*;
import java.util.*;
public class test{
public static void main(String args[]) {
File fin = new File ("matrix1.txt");
Scanner scanner = new Scanner(fin);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
}
}
}
Try putting the absolute path to the file, like
c:\\java\\matrix1.txt or /home/user/java/matrix1.txt
=== OOPS
You need to catch the Exception that's being thrown. Here's a couple options:
import java.io.*;
import java.util.*;
public class test{
public static void main(String args[]) throws FileNotFoundException {
File fin = new File ("matrix1.txt");
Scanner scanner = new Scanner(fin);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
}
}
}
OR
import java.io.*;
import java.util.*;
public class test{
public static void main(String args[]) {
File fin = new File ("matrix1.txt");
Scanner sc = null;
try {
scanner = new Scanner(fin);
}
catch(FileNotFoundException e) {
System.out.println("File does not exist...");
return;
}
while (scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
}
}
}
Make sure matrix1.txt is in your src folder if you're using Eclipse.
If you're using an IDE such as Netbeans/Eclipse, you need to put the file to be read in the project folder. This is usually 1 level above the src folder.
A good alternative in case you can't find the folder is to try and create a file. That way, you know where the file was created and you can place the file you want to read in that same folder.