Questions tagged [javac]

javac is the primary Java compiler, included in the Java Development Kit (JDK) from Sun Microsystems (now Oracle), and also in versions from other vendors.

javac is the primary Java compiler, included in the Java Development Kit (JDK) from Sun Microsystems (now Oracle), and also in versions from other vendors.

The compiler accepts source code conforming to the Java language specification (JLS) and produces bytecode conforming to the Java Virtual Machine Specification (JVMS). javac is itself written in Java. The compiler can also be invoked programmatically.

2467 questions
59
votes
2 answers

Difference in behaviour of the ternary operator on JDK8 and JDK10

Consider the following code public class JDK10Test { public static void main(String[] args) { Double d = false ? 1.0 : new HashMap().get("1"); System.out.println(d); } } When running on JDK8, this code prints…
SerCe
  • 5,826
  • 2
  • 32
  • 53
59
votes
2 answers

In Java 8, why were Arrays not given the forEach method of Iterable?

I must be missing something here. In Java 5, the "for-each loop" statement (also called the enhanced for loop) was introduced. It appears that it was introduced mainly to iterate through Collections. Any collection (or container) class that…
Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
59
votes
9 answers

is it possible to disable javac's inlining of static final variables?

The Java static compiler (javac) inlines some static final variables and brings the values directly to the constant pool. Consider the following example. Class A defines some constants (public static final variables): public class A { public…
sjlee
  • 7,726
  • 2
  • 29
  • 37
59
votes
10 answers

Is this valid Java?

Is this valid Java? import java.util.Arrays; import java.util.List; class TestWillThatCompile { public static String f(List list) { System.out.println("strings"); return null; } public static Integer…
lacroix1547
  • 1,060
  • 1
  • 8
  • 16
55
votes
2 answers

Why does the Java compiler 11 use invokevirtual to call private methods?

When compiling the code below with the Java compiler from OpenJDK 8, the call to foo() is done via an invokespecial, but when OpenJDK 11 is used, an invokevirtual is emitted. public class Invoke { public void call() { foo(); } private…
Julien
  • 2,139
  • 1
  • 19
  • 32
54
votes
7 answers

Using internal sun classes with javac

Is there a way to disable restrictions of javac 1.6.0_22 that prevent me from using JRE internal classes like sun.awt.event.* ? I'm not looking for: an explanation why it is forbidden. suggestion to use different classes suggestion to use…
Marcin Wisnicki
  • 4,511
  • 4
  • 35
  • 57
53
votes
3 answers

Why does javac allow some impossible casts and not others?

If I try to cast a String to a java.util.Date, the Java compiler catches the error. So why doesn't the compiler flag the following as an error? List strList = new ArrayList<>(); …
Mike Woinoski
  • 3,123
  • 1
  • 16
  • 15
53
votes
1 answer

How to get Intellij Idea to display compilation warnings?

I'm working with Intellij Idea 10 and Java 6 JDK Update 7. When I run Build --> Rebuild Project command, and the (javac) compilation generates warnings, Idea doesn't display what those warnings exactly are in the Messages view. I just see an…
Tommi
  • 8,550
  • 5
  • 32
  • 51
53
votes
1 answer

What is sjavac, who is it for and how do I use it?

There has been some buzz about a tool called sjavac on the OpenJDK mailing lists. Also, there are two related JEPs: JEP 139: Enhance javac to Improve Build Speed and JEP 199: Smart Java Compilation, Phase Two. My questions are: What exactly is the…
aioobe
  • 413,195
  • 112
  • 811
  • 826
52
votes
7 answers

Setting up enviroment variables in Windows 10 to use java and javac

I got a new laptop with Windows 10 and I want to set it up so I can use java and javac from the command line. I have searched online but all the guides are for previous versions, and I don't want to mess around with something I don't understand.
kilmarta
  • 629
  • 1
  • 6
  • 4
48
votes
4 answers

How do I set -Dfile.encoding within ant's build.xml?

I've got java source files with iso-8859-1 encoding. When I run ant, I get "warning: unmappable character for encoding UTF-8". I can avoid this if I run ant -Dfile.encoding=iso-8859-1 or add encoding="ISO-8859-1" to each javac statement. Is there…
neu242
  • 15,796
  • 20
  • 79
  • 114
47
votes
3 answers

what's the difference between -source and -target compatibility?

When using the Java compiler (javac), we can specify two kinds of compatibility. One is using -source and the other is using -target. What is the difference between these two? For example, -source 1.5 and -target 1.6? Also, is there any case where…
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
46
votes
7 answers

Compile code fully in memory with javax.tools.JavaCompiler

I'm using the JavaCompiler from the javax.tools package (JDK 1.7) to compile some stuff on the fly, like this: compiler.run(null, null, "-cp", paths, "path/to/my/file.java"); It works but I would like to do it all in memory (e.g. pass a string with…
Sandman
  • 2,577
  • 2
  • 21
  • 32
44
votes
5 answers

How can I set my Cygwin PATH to find javac?

I have a Windows 7 system on which I have installed the latest Java compiler. I also have the latest Cygwin. I want to use the Java compiler from Cygwin's shell. I edited the PATH variable in Cygwin as follows: export…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
44
votes
5 answers

How to pass -parameters javac flag to Java compiler via Gradle?

I have a Gradle-managed multi-project setup that relies on the new Java 8 -parameters compiler flag. I need 2 ways of including the compiler flag: To test classes only (the main project should compile without parameter names attached). To all…
Czyzby
  • 2,999
  • 1
  • 22
  • 40