0

Could you please someone explain what is the wrong thing when using javac. Because this class is compiled with Eclipse successfuly. I give the following CLI command:

pardus@adige:/mylinuz/pocws/src/test/swing/component$ javac -verbose SortButtonRenderer.java
[parsing started SortButtonRenderer.java]
[parsing completed 12ms]
[search path for source files: .]
[search path for class files: /usr/lib/jvm/java-6-oracle/jre/lib/resources.jar,/usr/lib/jvm/java-6-oracle/jre/lib/rt.jar,/usr/lib/jvm/java-6-oracle/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-6-oracle/jre/lib/jsse.jar,/usr/lib/jvm/java-6-oracle/jre/lib/jce.jar,/usr/lib/jvm/java-6-oracle/jre/lib/charsets.jar,/usr/lib/jvm/java-6-oracle/jre/lib/modules/jdk.boot.jar,/usr/lib/jvm/java-6-oracle/jre/classes,/usr/lib/jvm/java-6-oracle/jre/lib/ext/dnsns.jar,/usr/lib/jvm/java-6-oracle/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-6-oracle/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-6-oracle/jre/lib/ext/sunjce_provider.jar,.]
[loading java/awt/Component.class(java/awt:Component.class)]
[loading javax/swing/JLabel.class(javax/swing:JLabel.class)]
[loading javax/swing/JTable.class(javax/swing:JTable.class)]
SortButtonRenderer.java:8: package sun.swing.table does not exist
import sun.swing.table.DefaultTableCellHeaderRenderer;
                      ^
SortButtonRenderer.java:10: cannot find symbol
symbol: class DefaultTableCellHeaderRenderer
public class SortButtonRenderer extends DefaultTableCellHeaderRenderer {
                                        ^
[loading java/lang/Object.class(java/lang:Object.class)]
[checking test.swing.component.SortButtonRenderer]
SortButtonRenderer.java:17: cannot find symbol
symbol  : variable super
location: class test.swing.component.SortButtonRenderer
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        ^
[loading javax/swing/JComponent.class(javax/swing:JComponent.class)]
[loading java/awt/Container.class(java/awt:Container.class)]
[loading java/awt/image/ImageObserver.class(java/awt/image:ImageObserver.class)]
[loading java/awt/MenuContainer.class(java/awt:MenuContainer.class)]
[loading java/io/Serializable.class(java/io:Serializable.class)]
[loading javax/swing/TransferHandler.class(javax/swing:TransferHandler.class)]
[loading javax/swing/TransferHandler$HasGetTransferHandler.class(javax/swing:TransferHandler$HasGetTransferHandler.class)]
[loading javax/swing/SwingConstants.class(javax/swing:SwingConstants.class)]
[loading javax/accessibility/Accessible.class(javax/accessibility:Accessible.class)]
SortButtonRenderer.java:19: cannot find symbol
symbol  : method setHorizontalAlignment(int)
location: class test.swing.component.SortButtonRenderer
        setHorizontalAlignment(JLabel.CENTER);
        ^
[loading java/lang/String.class(java/lang:String.class)]
SortButtonRenderer.java:20: cannot find symbol
symbol  : method setText(java.lang.String)
location: class test.swing.component.SortButtonRenderer
        setText((value == null) ? "" : value.toString());
        ^
SortButtonRenderer.java:21: incompatible types
found   : test.swing.component.SortButtonRenderer
required: java.awt.Component
        return this;
               ^
[total 297ms]
6 errors

Interesting thing is that The missing class, sun.swing.table.DefaultTableCellHeaderRenderer, is available in rt.jar and compiler can load other classes such as java/awt/Color.class from rt.jar.

package test.swing.component;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import sun.swing.table.DefaultTableCellHeaderRenderer;

public class SortButtonRenderer extends DefaultTableCellHeaderRenderer {
    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        setHorizontalAlignment(JLabel.CENTER);
        setText((value == null) ? "" : value.toString());
        return this;
    }
    //other methods removed
}

Note that: When I run java8's javac still error occurs. But java8's javac with "endorseddirs /usr/lib/jvm/java-6-oracle/jre/lib" is OK.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mithat Bozkurt
  • 547
  • 5
  • 17
  • Start by not importing classes in the `sun.*` hierarchy of packages. They are not intended for programmer use. – Andrew Thompson Jun 11 '18 at 19:58
  • This is a truncated class from legacy huge project written at 2003 and i couldn't migrate it. You are correct at using sun.* classes but the why is compiled last situation? – Mithat Bozkurt Jun 11 '18 at 20:05
  • *"but the why is compiled last situation?"* I both don't know ***and*** don't care. Fix the legacy project. – Andrew Thompson Jun 11 '18 at 20:10

2 Answers2

0

Are you certain the command line and eclipse javac are running the same version? (Going off of memory but options->search javac should show you which compiler) and javac -version will give you the correct version on the command line.

EDIT: yeah, and definitely get rid of the sun.* imports! They will only lead to more pain.

bowzerfood
  • 29
  • 4
  • Actually I don't know where i should look for eclipse javac version. But As far as i understand eclipse use its own compiler implementation. At here https://stackoverflow.com/questions/3061654/what-is-the-difference-between-javac-and-the-eclipse-compiler – Mithat Bozkurt Jun 11 '18 at 20:29
  • eclipse uses a pre-installed jdk. I know because I had a JAVA_HOME pointing to a new jre before starting and chaos ensued. Search your preferences/settings for jdk and you'll see it. Try and compile the project with that jdk. – bowzerfood Jun 13 '18 at 05:55
  • Eclipse does use a pre-installed jdk based on JAVA_HOME not a co-installed JDK. Kind of a silly decision honestly. The amount of people who run into JAVA_HOME pointing to a JRE instead of a JDK is high. I am pretty sure you are using different JDK or because the output is different. echo $JAVA_HOME for Linux or echo %JAVA_HOME% for windows. – bowzerfood Jun 14 '18 at 13:03
0

if you need to keep a sun.* class and are packaging using maven, add this configuration to maven-compiler-plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgs>
                    <arg>-XDignore.symbol.file</arg>
                </compilerArgs>
                <fork>true</fork>
        </configuration>
    </plugin>    
Adrian Faur
  • 61
  • 1
  • 2