我是一名尝试学习 Java 的 PHP/Mysql 程序员,但我一直坚持如何在其中编译文件:
public static void main(String[] args)
这是示例代码:
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();
}
}
}
当运行尝试在 Windows 命令提示符下使用 javac 命令将其从 .java 文件编译为要在网页中调用的 .class 文件时,我收到一条错误消息:
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) {
如果像这样编译一个 .java 文件:
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;
}
}
我没有收到任何错误,.java 文件编译成 .class 文件,我可以在网页中使用 .class 文件就好了。
不知道我做错了什么。
多谢你们!
好的,所以现在,当我运行这个命令时:
C:\ > javac GreetingClient.java
我明白了:
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
同样,我似乎只在对包含以下行的文件运行 javac 命令时出现错误:
public static void main(String[] args)
我知道我错过了一些东西,任何帮助将不胜感激。