javac

Why does the Java Compiler copy finally blocks?

旧街凉风 提交于 2019-12-03 08:33:27
问题 When compiling the following code with a simple try/finally block, the Java Compiler produces the output below (viewed in the ASM Bytecode Viewer): Code: try { System.out.println("Attempting to divide by zero..."); System.out.println(1 / 0); } finally { System.out.println("Finally..."); } Bytecode: TRYCATCHBLOCK L0 L1 L1 L0 LINENUMBER 10 L0 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "Attempting to divide by zero..." INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;

【javac添加python 列表特性10】为Block添加返回值

帅比萌擦擦* 提交于 2019-12-03 08:24:18
前面几篇提到的一些特性,都是通过把特殊的新语法,翻译成java可以支持的语法来实现的。javac编译一个文件的过程为:(参见 javac编译文件过程 ) Scanner->Parser->Annotation(optional)->Enter->Attribute->Flow->Desugar->Generate 那么之前我的工作都是集中在Parser和Attribute上面的。添加新语法的时候,就是把Parser修改一下,使其能够识别新语法为一个新的节点,然后Annotation,Enter,Attribute基本不变,在Attribute过后可以获得类型等额外信息,然后对这个时候的AST运用一个Translator,把新语法转变为旧的语法。因此Flow和以后的过程都没有任何改变。 而这次是涉及到全过程的改变。这次我想实现的是这样的语法: int r1=10,r2=100; double area; double sum={double PI=Math.PI; area=PI*r1*r1;} + {double PI=Math.PI; area=PI*r2*r2;}; 其实就是把Block作为一个可以有值的结构,他的值就是block最后一个语句的值。block依然是原来的功能,可以有任何的语句,block的结果也可以参与运算。 只是这是一个实实在在的新语法,不能通过翻译来解决问题

【javac添加python 列表特性8】list assignment和list下标为负数的...

怎甘沉沦 提交于 2019-12-03 08:23:43
为了支持给列表进行赋值,使用这样的语法 list[1]=10, 而这跟a=list[1]是不一样的,因为前者用java的语法应该翻译成 list.set(1,10),而后在应该翻译成 a= list.get(1)。所以要判断list[1]是在赋值的左边还是右边。 对于list[1]=10,抽象语法树的遍历过程是 JCAssign(list[1]=10) -> JCIndexed(list[1]) ->JCLiteral(10) JCAssign(list[1]=10) <- 采用后序遍历,先子树,后父树 所以采用一个全局变量来记录当前是不是在左边(isLeft)和一个变量记录当前是不是list的赋值(isSet), boolean tmpIsLeft = isLeft; // save isLeft states, to solve nested // problem such as list[list[1]=a]=a; isLeft = true; // set isLeft=true boolean tmpIsSet = isSet; // save isSet states isSet = false; tree.lhs = translate(tree.lhs); // translate left tree of assign isLeft = false; tree

Javac vs Java within -classpath option

送分小仙女□ 提交于 2019-12-03 06:03:13
What is the difference in calling the -classpath option from javac and from java for example: javac -classpath MyJar.jar GetJar.java java -classpath MyJar.jar:. GetJar it works as well as: javac -classpath MyJar.jar GetJar.java java GetJar So basically where the first -classpath related to javac needs to be there, on the other hand in the java command line it might be optional. Why? Do you know in which circumstance it would be mandatory. And more in general what is the effect of -classpath called by javac and what is the effect of -classpath called by java . Thanks in advance. One is the

Lower-bounded wild card causes trouble in javac, but not Eclipse

删除回忆录丶 提交于 2019-12-03 06:00:50
This piece of code compiles in Eclipse but not in javac: import java.util.function.Consumer; public class Test { public static final void m1(Consumer<?> c) { m2(c); } private static final <T> void m2(Consumer<? super T> c) { } } javac output: C:\Users\lukas\workspace>javac -version javac 1.8.0_92 C:\Users\lukas\workspace>javac Test.java Test.java:5: error: method m2 in class Test cannot be applied to given types; m2(c); ^ required: Consumer<? super T> found: Consumer<CAP#1> reason: cannot infer type-variable(s) T (argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super T>)

Building Java package (javac to all files)

旧街凉风 提交于 2019-12-03 05:36:51
How to compile all files in directory to *.class files? Well, this seems pretty obvious, so I may be missing something javac *.java (With appropriate library references etc.) Or perhaps: javac -d bin *.java to javac create the right directory structure for the output. Were you looking for something more sophisticated? If so, could you give more details (and also which platform you're on)? Yet another way using "find" on UNIX is described here: http://stas-blogspot.blogspot.com/2010/01/compile-recursively-with-javac.html The following two commands will compile all .java files contained within

Compile in Java 6, run in 7 - how to specify useLegacyMergeSort?

限于喜欢 提交于 2019-12-03 05:10:43
I'm wondering if I compile in Java 6, but someone runs the program on Java 7, will the Java 6 or 7 version of Arrays.sort be used? It's important because the new mergesort throws an IllegalArgumentException, and the old one doesn't (see Comparison method violates its general contract! Java 7 only ) Now, it's possible to compile in Java 7 using Arrays.useLegacyMergeSort, but obviously that flag isn't available for Java 6 - and we want to be compatible on Mac OS Snow Leopard (which uses 6). For some reason (see http://madbean.com/2006/target14/ ) the -target compiler flag doesn't seem to produce

Resolving version conflict between java and javac on Ubuntu

早过忘川 提交于 2019-12-03 05:02:36
问题 I have a problem with my compiled Java application on Ubuntu. It throws UnsupportedClassVersionError . I am compiling with a higher JDK version than the one that is configured in my PATH to run Java: $ javac -version javac 1.7.0_147 $ java -version java version "1.6.0_23" OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2) OpenJDK Client VM (build 20.0-b11, mixed mode, sharing) How can I resolve this? 回答1: Run either of the following to locate where the location is of

What is the difference between using javac and javax.tools.JavaCompiler?

放肆的年华 提交于 2019-12-03 03:37:41
问题 Maven Compiler Plugin documentation states: The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse And indeed when forceJavacCompilerUse is not specified in our build there are some build errors, for example when the code references the com.sun. packages

Compiling and running java application using powershell

梦想的初衷 提交于 2019-12-03 00:43:31
I am trying to compile and a sample Helloworld.java file. I have my jdk installed in C:\Program Files\jdk1.7\bin. And I have my Helloworld.java in C:\Helloworld.java I am actually a novice in both powershell and java. I got some examples from web regarding this but many of them advice to run it like this: java.exe -classpath $Env:CLASSPATH C:\Helloworld.java But when I give this in powershell I get an error called 'CLASSPATH' is not defined even after adding it in env variables. And when I try to compile the code with the following syntax: $javac C:\Helloworld.java I get an error "javac is not