找不到符号:java.net.Socket

时间:2009-09-11 20:12:52

标签: java macos

在处理更大的项目时,使用java.net.Socket的几行代码无法编译。这是在Mac 10.5.5上使用Java for Mac OS X 10.5 Update 4(java version "1.5.0_20")。其他东西(awt,ssl工厂,踏板等)仍然可以编译和运行。我将违规代码删到了一个小班Fake.java

import java.net.Socket;
public class Fake {
  public String sendMsg() {
    Socket so;
    try {      
      so = Socket("www.yahoo.com", 80);
    } catch (Exception e) {
    } finally {
    }
    return "";      
  }
}

如果我使用javac -verbose Fake.java编译它,它会从jar文件加载Socket.class,然后报告Fake.java:6: cannot find symbol。类路径似乎都是正确的,所以我不确定这是一个令人讨厌的问题。完整的编译输出是:

$ javac -verbose Fake.java 
[parsing started Fake.java]
[parsing completed 41ms]
[search path for source files: [.]]
[search path for class files: [/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/ui.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/laf.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jsse.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar, /Users/shamma/Library/Java/Extensions/js.jar, /System/Library/Java/Extensions/AppleScriptEngine.jar, /System/Library/Java/Extensions/CoreAudio.jar, /System/Library/Java/Extensions/dns_sd.jar, /System/Library/Java/Extensions/j3daudio.jar, /System/Library/Java/Extensions/j3dcore.jar, /System/Library/Java/Extensions/j3dutils.jar, /System/Library/Java/Extensions/jai_codec.jar, /System/Library/Java/Extensions/jai_core.jar, /System/Library/Java/Extensions/mlibwrapper_jai.jar, /System/Library/Java/Extensions/MRJToolkit.jar, /System/Library/Java/Extensions/QTJava.zip, /System/Library/Java/Extensions/vecmath.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/apple_provider.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/dnsns.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/localedata.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/sunjce_provider.jar, /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/sunpkcs11.jar, .]]
[loading /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar(java/net/Socket.class)]
[loading /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar(java/lang/Object.class)]
[loading /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar(java/lang/String.class)]
[checking Fake]
Fake.java:6: cannot find symbol
symbol  : method Socket(java.lang.String,int)
location: class Fake
      so = Socket("www.yahoo.com", 80);
           ^
[loading /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar(java/lang/Exception.class)]
[loading /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar(java/lang/Throwable.class)]
[total 282ms]
1 error

奇怪的是它正在寻找Socket()类中的Fake方法。如果我将第6行编辑为:

so = java.net.Socket("www.yahoo.com", 80);

错误变为:

Fake.java:6: cannot find symbol
symbol  : class net
location: package java
      so = java.net.Socket("www.yahoo.com", 80);
               ^
1 error

3 个答案:

答案 0 :(得分:4)

这是一个黑暗的总镜头,但你应该在类型之前有新的关键字吗?

so = Socket("www.yahoo.com", 80);

应该是

so = new Socket("www.yahoo.com", 80);

答案 1 :(得分:1)

应该是:

so = new Socket(...)

答案 2 :(得分:1)

so = Socket("www.yahoo.com", 80);

应该是

so = new Socket("www.yahoo.com", 80);

问候,Jan