简体   繁体   中英

gradle - “compileJava” not compiling Java because “UP-TO-DATE”?

Let's say I have a simple class

public class A {
    public int inc(int k, int i){
        System.out.println(i);
        return 0;
    }
}

And I call it from Main

public class Main {
    public static void main(String[] args) {
        new A().inc(0,7);
    }
}

The corresponding java bytecode ( javap -c A.class ):

public class A {
  public A();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public int inc(int, int);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: iload_2
       4: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
       7: iconst_0
       8: ireturn
}

Note the iload_2 .

Now let's remove the unnecessary parameter k .

public class A {
    public int inc(int i){
        System.out.println(i);
        return 0;
    }
}

and of course

public class Main {
    public static void main(String[] args) {
        new A().inc(7);
    }
}

Now if I re-run the code, A will be compiled to

public class A {
  public A();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public int inc(int);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: iload_1
       4: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
       7: iconst_0
       8: ireturn
}

Which is what I'd expect. However, let's say I don't want to re-run everything and am just interested in the bytecode-level changes ...

gradle compileJava

and then

javap -c A.class

it will not have changed.

And in fact, upon checking gradle output, there's a

:compileJava UP-TO-DATE

There that clearly doesn't belong there.

Now, I've created the project with the Intellij (2017.2.4) wizard, (gradle project with Java as "additional libraries and frameworks", using SDK1.8), added the Main and A classes, and tested this.

What do I need to change to make this work the way it's supposed to?

Ok, so it's not an issue with Gradle, but rather a misconfiguration of Intelij's.

When creating the project,

Gradle will compile to .\\build\\classes ,

but the "module compile output path" in project structure -> modules is set to .\\out\\production\\classes - which is from where Intellij will take the bytecode and run the classes.

It's also where recompiling Gradle doesn't touch any files.

I've opened a different question on how to deal with that, as this answers this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM