Reputation: 97
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.
Upvotes: 2
Views: 4643
Reputation: 518
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
Upvotes: 0
Reputation: 992985
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.
Upvotes: 5