1

This might be a very stupid question but bear with me since I am new to Java/J2EE. I have installed JRE6 and JRE7 on my machine. I have set JAVA_HOME environment variable to point to my JRE6 directory. Also, in my eclipse, I have pointed the working JRE to JRE6. However, if run "java -version" in the terminal, it says current java version is 1.7 (pointing to JRE7).

The problem I am actually facing is that eclipse gives me errors for the following iunterface:-

public interface IServiceHelper {
    public <T extends Document> Document SomeMethod(Object obj);

          // Error - Duplicate method SomeMethod(Object) in type IServiceHelper
    public <T extends SomeClass> SomeClass SomeMethod(Object obj);  

          // Error - Duplicate method SomeMethod(Object) in type IServiceHelper
    public <T extends String> String SomeMethod(Object obj);
}

However, a maven build outside of eclipse in a terminal succeeds.

The above interface may not be a good design and may be really some tricky code. Since this is an existing piece of code, I am not supposed to make changes. Please help me find out what is wrong with eclipse and/or JRE version.

Higher-Kinded Type
  • 1,964
  • 5
  • 27
  • 44
  • The exact error message is "Duplicate method SomeMethod(Object) in type IServiceHelper". – Higher-Kinded Type Jun 19 '12 at 05:44
  • Although all sources to my little knowledge point to JRE6, and error is due to JRE7. If that's not correct, what is the source of the error? BTW THERE ARE NO ERRORS AND CODE WORKS FINE ON MY COUNTERPART'S MACHINE (with Eclipse and JRE6) – Higher-Kinded Type Jun 19 '12 at 05:45
  • 3
    I really don't see how this could ever compile: you define three methods with the same signature in the same interface. – JB Nizet Jun 19 '12 at 05:48
  • But it does compile and run on my counterpart's machine. I am not Java expert but it seems you can fool the compiler by making it generic and the compiler assumes it is overloaded. My understanding was that overloading does not consider return types. However, it is otherwise in this case. – Higher-Kinded Type Jun 19 '12 at 06:29

2 Answers2

2

There are a couple of things here:

  1. I assume you are using Windows. See this thread for understanding why it ignores your JAVA_HOME - Windows ignores JAVA_HOME: how to set JDK as default?
  2. Using the same method name and arguments with a different return type is forbidden in Java, see a discussion here so in java you can't have duplicate method names with different return and params?
  3. Your maven build works? Interesting. Can you run 'clean' before building, build in debug and share the results?
Community
  • 1
  • 1
Oded Peer
  • 2,377
  • 18
  • 25
  • Point was useful and for a test I replaced the binaries in System32 folder with the Java 1.6 binaries. And it works as expected. However the point #2 seems to be true only theoritically. As said above, my counterpart(s) machine does not report error in eclipse too. And even the maven build with Java 1.6 compiler works without any errors. So how do I make it work with eclipse too? – Higher-Kinded Type Jun 19 '12 at 07:46
  • The second item is backed up in the Java Language Specification: http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4.2 It is a compile-time error to declare two methods with override-equivalent signatures (defined below) in a class. Two methods have the same signature if they have the same name and argument types. – Oded Peer Jun 20 '12 at 05:31
0

If you have mess with Java versions on your machine you can easily set up which Java eclipse will use. Edit file /eclipse.ini Add: -vm PATH_TO_YOUR_JAVAW_EXECUTABLE

From Eclipse FAQ: If a JVM is installed in the eclipse/jre directory, Eclipse will use it; otherwise the launcher will consult the eclipse.ini file and the system path variable. Eclipse DOES NOT consult the JAVA_HOME environment variable.

To explicitly specify a JVM of your choice, you can use the -vm command line argument:

eclipse -vm c:\jre\bin\javaw.exe ''start Java by executing the specified java executable

http://wiki.eclipse.org/FAQ_How_do_I_run_Eclipse%3F

Maciej
  • 140
  • 3
  • The Eclipse JRE thing is solved, in a way. So how do I get it compiled (the code in question)? – Higher-Kinded Type Jun 19 '12 at 11:50
  • I did not try to compile that code yet. First of all - check if you have the same JRE both in command line and in Eclipse(using which JRE your eclipse is running Help -> About -> Installation details -> Configuration) and WHAT IS IMPORTANT HERE - what JDK you set up for Eclipse to use when compiling projects. Take a lookt in Eclipse settings: Go to the Eclipse Preferences -> Java -> Installed JREs. And also check level of compilation both in Eclipse preferences and in PROJECT SETTINGS because you can overwrite global settings at project's level. – Maciej Jun 19 '12 at 12:17
  • Yes the JRE versions are same. But the compilation problem still exists. – Higher-Kinded Type Jun 20 '12 at 02:38