javac:无效的目标版本:1.7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22493736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javac: invalid target release: 1.7
提问by davedave
Using OS X Mavericks, and after upgrading my JDK, I can no longer compile with ant.
使用 OS X Mavericks,升级 JDK 后,我无法再使用 ant 进行编译。
I've done the usual googling and the vast majority of answers point to JAVA_HOME not being set.
我已经做了通常的谷歌搜索,绝大多数答案都指向 JAVA_HOME 没有设置。
Help me stackoverflow, you're my only hope!
帮助我stackoverflow,你是我唯一的希望!
Information which may be useful
可能有用的信息
$ tail -n1 /etc/profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
$ which javac
/usr/bin/javac
$ ls -lah /usr/bin/javac
lrwxr-xr-x 1 root wheel 75B 8 Jan 11:23 /usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
$ ls -lah /System/Library/Frameworks/JavaVM.framework/Versions/
total 80
drwxr-xr-x 13 root wheel 442B 19 Mar 10:10 .
drwxr-xr-x 12 root wheel 408B 19 Mar 09:59 ..
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 8 Jan 11:23 1.6.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 19 Mar 10:10 1.7 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10B 19 Mar 10:10 1.7.0 -> CurrentJDK
drwxr-xr-x 8 root wheel 272B 8 Jan 11:23 A
lrwxr-xr-x 1 root wheel 1B 8 Jan 11:23 Current -> A
lrwxr-xr-x 1 root wheel 58B 19 Mar 10:10 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents
$ javac -version -source 1.7 -target 1.7 -fork true
javac 1.7.0_51
javac: invalid source release: 1.7
Usage: javac <options> <source files>
采纳答案by davedave
回答by David W.
I'm on OS X Mavericks too with Java 1.7.0_51 from Oracle. There's no -fork
option, but everything else seems to work fine:
我也在 OS X Mavericks 上使用 Oracle 的 Java 1.7.0_51。没有-fork
选择,但其他一切似乎都正常:
HelloWorld.java
你好世界
public class HelloWorld
{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Command:
命令:
$ javac -version -source 1.7 -target 1.7 -fork true *.java
javac 1.7.0_51
javac: invalid flag: -fork
Usage: javac <options> <source files>
use -help for a list of possible options
$ javac -version -source 1.7 -target 1.7 *.java
javac 1.7.0_51
$ # Everything compiled...
Are you using OpenJDK or the official Oracle release? It may be possible that the OpenJDK doesn't recognize version 1.7 yet. I know that the OpenJDK use to be behind the official Oracle release.
您使用的是 OpenJDK 还是 Oracle 官方版本?OpenJDK 可能还不能识别 1.7 版。我知道 OpenJDK 曾经落后于正式的 Oracle 版本。
- What happens if you leave the
-source
and-target
out? - What if you have only one or the other? Does it work with
1.6
instead of1.7
?
- 如果你离开
-source
和离开会发生什么-target
? - 如果你只有一个或另一个怎么办?它适用于
1.6
而不是1.7
吗?
The error message you get is the same error message I get when I try 1.8
. Are you trying to compile from the command line, or from Eclipse, or from Ant?
您收到的错误消息与我尝试时收到的错误消息相同1.8
。您是尝试从命令行、Eclipse 还是 Ant 进行编译?
One more final possibility: The .
isn't a period:
还有最后一种可能性:这.
不是句号:
$ javac -version -source 1?7 -target 1.7 *.java
javac: invalid source release: 1?7
Usage: javac <options> <source files>
use -help for a list of possible options
That's not a period. It's a small full stope2:80:a4
and not a full stop/period(2e
):
那不是一个时期。这是一个小的句号,e2:80:a4
而不是句号/句点( 2e
):
0000000 j a v a c - v e r s i o n -
6a 61 76 61 63 20 2d 76 65 72 73 69 6f 6e 20 2d
0000020 s o u r c e 1 ? ** ** 7 - t a
73 6f 75 72 63 65 20 31 e2 80 a4 37 20 2d 74 61
0000040 r g e t 1 . 7 * . j a v a \n
72 67 65 74 20 31 2e 37 20 2a 2e 6a 61 76 61 0a
0000060
回答by Fran?ois Romain
The solution in the accepted answer didn't work for me; I got the error:
接受的答案中的解决方案对我不起作用;我得到了错误:
tools.jar: No such file or directory.
tools.jar:没有这样的文件或目录。
However, I found a working solution here. In short, I installed the latest version of the jdk from Oracle websiteand this solves the problem.
但是,我在这里找到了一个可行的解决方案。简而言之,我从 Oracle 网站安装了最新版本的 jdk,这解决了问题。