tags:

views:

453

answers:

3

While working on a larger project, a few lines of code using java.net.Socket failed to compile. This is on a Mac 10.5.8 with Java for Mac OS X 10.5 Update 4 (java version "1.5.0_20"). Other things (awt, ssl factories, treads, etc) still compile and run. I pulled the offending code down into a small class 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 "";      
  }
}

If i compile this with javac -verbose Fake.java it loads the Socket.class from the jar file but then reports Fake.java:6: cannot find symbol. The classpaths all seem correct, so I'm not sure whats the offending issue. The full compile output is:

$ 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

Oddly its looking for the Socket() method in the Fake class. If I edit line 6 to:

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

The error becomes:

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

This is a total shot in the dark, but should you have the new keyword before the type?

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

should be

so = new Socket("www.yahoo.com", 80);
inkedmn
Wait, so you're saying he's not trying to call the method `Socket` in the class `net` in the namespace `java`? Of course, that class doesn't exist...
R. Bemrose
D'oh, I was trying to call it statically as a factory.
ayman
A: 

It should be:

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

sould be

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

Regards, Jan

Jan