忽略编译错误 - Java

发布于 2024-12-07 05:56:01 字数 269 浏览 1 评论 0原文

我有大约 1500 个文件需要编译,其中 15-20 个文件有编译错误。这些文件不在我的控制之下,所以我无法进行任何修改/更新/删除。所以,我这里有两个问题。

1) 我如何忽略这 15-20 个文件中的编译错误并继续为其余文件生成 .class 文件。是否有任何 javac 命令行选项或任何将忽略编译错误并为所有其他非错误文件生成 .class 文件的选项。

2)java编译器会在看到这些错误时立即中止编译,还是会继续编译(生成.class文件)其他所有内容,最后抱怨这些文件有错误。

I have around 1500 files to compile, in which 15-20 files have compilation errors. These files are not under my control, so I could not do any modification/update/delete. So, i have two questions here.

1) how do i ignore the compilation errors from these 15-20 files and continue to produce the .class file for rest of them. is there any javac commandline option or anything which will ignore the compliation errors and produce the .class files for all other non error files.

2)will the java compiler abort compilation as soon as it sees these errors or will it continue compiling(producing .class files) everything else and at the end then complain about these files with errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等待我真够勒 2024-12-14 05:56:01

您可以使用 Eclipse。它的内部编译器 - 至少在某些情况下 - 能够继续构建的其余部分,即使某些类没有完全编译。如果可能的话,它甚至会为损坏的类生成类文件,生成在调用时立即抛出异常的方法。

强烈建议您只需复制所有源代码并尽早修复至少在您自己的副本中的错误,但是 Eclipse 的部分编译可能可以帮助您。

You can use Eclipse. Its internal compiler is - at least in some cases - able to keep going with the rest of the build, even when some classes don't compile fully. It will even produce class files for the broken classes if possible, generating methods which throw an exception as soon as they're called.

I would strongly recommend that you simply take a copy of all the source and fix the errors at least in your own copy as early as possible, but Eclipse's partial compilation may help you.

吻泪 2024-12-14 05:56:01

您不能忽略编译错误。他们总是会导致构建失败。

您唯一的选择是与控制文件的人交谈以修复它们或找到替换它们的方法。

如果您尝试从构建中删除它们,您还必须删除使用这些文件的所有文件。例如

class A {
    B b;
}

,如果 B 有编译错误,您的构建脚本可以跳过 B.java,但是当您点击 A.java 时,它无论如何都会尝试编译 B,因此必须删除 A。这可能被证明是一项不平凡的任务。

You cannot ignore compile errors. They will always fail the build.

Your only choices are to talk to whoever controls the files to get them fixed or find a way to otherwise replace them.

If you try to remove them from the build, you will also have to remove any files that use those files. For example

class A {
    B b;
}

If B has a compile error, your build script can skip B.java, but when you hit A.java, it's going to try to compile B anyway, so A has to be removed. This may prove to be a non-trivial task.

百思不得你姐 2024-12-14 05:56:01

您可以自己编写一个脚本来遍历源代码树并为每个 java 文件单独调用 javac。这样,您最终将得到正确编译的所有文件,这些文件不依赖于有错误的文件。不过,这将是一个极其缓慢的操作。我预计它比单次调用 javac 花费的时间长 100 倍(考虑到最终会进行大约 1500 次调用)。

You could write yourself a script that traverses your source tree and calls javac for each java file individually. This way you will end up with all files compiled correctly that do not depend on the files with errors. This would be a horrendously slow operation, though. I would expect it to take several 100 times longer than a single call to javac (considering you would end up with about 1500 calls).

沫尐诺 2024-12-14 05:56:01

您可以使用 ant 任务中的排除标记从编译中排除某些源文件。

  <target name="compile" description="Compile Java source files">
    <javac destdir="classes" classpathref="classpath">
      <src path="src"/>
      <exclude name="**/excluded_folder/**"/>
    </javac>
  </target>

编辑:
当然,任何依赖于排除类的 java 文件也将无法编译,除非类路径中已经有排除文件的预编译版本。

You can exclude certain source files from compiling using an exclude tag in the ant task.

  <target name="compile" description="Compile Java source files">
    <javac destdir="classes" classpathref="classpath">
      <src path="src"/>
      <exclude name="**/excluded_folder/**"/>
    </javac>
  </target>

EDIT:
But of course any java files that are dependent on the excluded classes will also fail to compile unless you already have a pre-compiled version of excluded files in the classpath.

梦罢 2024-12-14 05:56:01

创建有问题的文件的模型。基本上与 C 头文件的想法相同,包括函数签名和合理的默认返回值(nullfalse0)。这将允许 javac 编译所有内容,只需确保模拟类不包含在最终发行版中,否则当它们首先出现在类路径中时,您将遇到奇怪的错误。这也适用于实现损坏的接口和继承损坏的类。

Create mockups of the offending files. Basically the same idea as a C header file, include the function signatures and reasonable default return values (null, false, 0). This will allow javac to compile everything, just make sure the mock classes don't get included in the final distribution, or you'll have strange bugs when they end up first in the classpath. This works for implementing broken interfaces and inheriting from broken classes too.

为你鎻心 2024-12-14 05:56:01

你是什​​么意思

它将忽略编译错误并为所有其他非错误文件生成 .class 文件

您是否意识到,如果这些文件相关(就像在单个项目中一样),那么依赖的源文件也将无法编译..?除了更正编译错误并重试之外,没有办法绕过这个问题!

What do you mean

which will ignore the compliation errors and produce the .class files for all other non error files

Do you realise that if these files are related ( as if in a single project ) then the dependent source files will not compile too.. ? There is no way to by-pass that except to correct the compilation errors and retrying !

夏末的微笑 2024-12-14 05:56:01
  1. 告诉破坏代码的人(当然是通过外交手段)尽快修复它。 其他人建议(参见第二注释)以使这些事件公开(在团队/公司内),这大大减少了此类编译错误的数量
  2. 注释所有阻止代码编译的内容(假设您不需要这些部分)

不应该处理未编译的代码,而应该处理提交代码的人。如果有更多的人致力于该项目,每个人都必须在他的本地计算机上处​​理同样的问题(找出它没有编译的原因,追踪错误的类,排除/注释这些类如果您的代码不依赖于它们,请重建等)只有 1 个人来解决问题会更有效,而其他人只需更新他的本地存储库。

  1. Tell the one who broke the code (diplomatically, of course) to fix it ASAP. Others have suggested (see 2nd comment there) to make these occurrences public (within a team/company), which greatly reduces the number of such compilation errors
  2. Comment everything that prevents the code from compiling (supposed that you don't need those parts)

You should not have to deal with not-compiling code, but the one who committed it. If there are more people working on that project, everyone has to deal with that same problem on his local machine (find out why it didn't compile, track down the erroneous classes, exclude/comment those classes if your code does not depend on them, rebuild, etc.) It is much more efficient to have only 1 person to fix the problem, and everyone else just updates his local repository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文