java file path join_如何抑制Java的警告,特定目录或文件,如生成的代码

展开全部

1. 从版本3.8 M6开始,Eclipse的(准确的说:JDT的)有内置32313133353236313431303231363533e58685e5aeb931333337613262的函数,这。它是可配置的,通过一个项目的构建路径:项目属性> Java构建路径>编译器>来源 在这里宣布:Eclipse的3.8和4.2的M6-新的和值得注意的是,所谓的选择性忽略源文件夹中的错误/警告。这也是那里的截图是从。这是链接错误220928开发新的函数。

2. 有这个一票,臭虫220928,具有自为Eclipse 3.8。请参考这个答案的细节。 上创建(或至少指向)一个名为“warningcleaner”在35插件票“马克”:如果你坚持使用Eclipse 3.7或更低。我认为有很多在等待这个函数被集成到Eclipse中。 这真的很简单: 安装插件。 右键单击项目并选择“添加/删除生成的代码自然”。 打开项目设置(右键单击并选择“属性”)。 打开选项卡“警告清洁'。 选择您想从忽略警告的源文件夹。

3. 我解决了这个maven的正则表达式替换插件-它并没有解决 CodeGo.net,但治愈的痛苦:

com.google.code.maven-replacer-plugin

maven-replacer-plugin

1.3.2

prepare-package

replace

target/generated-sources/antlr/**/*.java

true

MULTILINE

^public class

@SuppressWarnings("all") public class

请注意,我没能拿到**符号来工作,所以你可能需要准确地指定路径。 参见下面的如何不产生重复@SupressWarnings

4. 我认为最好的你能做的就是使项目的具体设置用于显示警告。 窗口->设定-> Java的->编译器->错误/警告 在窗体的顶部是一个链接,用于配置项目的具体设置。

5. 网友@乔恩暗示ant代码来做到这一点。下面是我的Adding @SuppressWarnings("all") to ANTLR generated parser/lexer *.java

in ${project.build.directory}/generated-sources/antlr/

summary="true"

includes="**/*.java"

token="public class"

value='@SuppressWarnings("all") public class' />

需要注意的是Ant的中并没有文本的正则表达式 所以它的令牌相匹配行的开始如maven的正则表达式替换插件一样。 我跑了Antlr从Maven的antrun-插件在我的Maven POM,ANTLR的maven插件没有用的Cobertura Maven插件踢好我这样做的。 (我知道这不是一个答案,原来的问题,但我不能在一个格式化的Ant代码到另一个答案,只在一个答案)

6. 我不认为Eclipse的本质提供了一种在目录级做到这一点(但我不知道)。 你可以有生成的文件去到一个单独的Java项目,并控制该特定项目的警告。 我一般喜欢把自动生成的代码在一个单独的项目呢。

7. 您只能抑制警告在项目级别。但是,您可以配置你的问题选项卡从文件或软件包抑制警告。进入配置,并与“关于工作组:”工作范围。

8. 我做这几个ANTLR的语法,它生成一个Javaant。 Ant构建脚本添加@SuppressWarnings("all")一个Java文件,并@Override到另一个。 我可以看看它是如何准确地做,如果你有兴趣。

9. 在ANTLR 2的情况下,有可能通过appenidng抑制在生成的代码中的警告@SuppressWarnings在类声明中的语法文件之前,例如:{@SuppressWarnings("all")} class MyBaseParser extends Parser;

10. 这可以通过从构建路径中排除特定的目录来完成(下面的例子就是Eclipse 3.5) [1]调出Java构建路径 点击projectin包资源管理器 右击,属性 选择Java Build Path [2]将目录添加到排除 来源标签应包含项目源文件夹的详细信息 展开源文件夹,并找到了“排除:'属性 选择“排除:”,然后单击编辑 添加文件夹到添加/添加多个选项 单击Finish(完成),然后确定为Eclipse重建。

11. 该M2E生成这个小python脚本“补丁”.classpath文件并添加所需的XML标记开始与所有的源文件夹target/generated-sources。你可以从你的项目的根文件夹中运行它。你需要重新运行它时,在Eclipse从M2E重新生成。和所有风险自负,;-)#!/usr/bin/env python

from xml.dom.minidom import parse

import glob

import os

print('Reading .classpath files...')

for root, dirs, files in os.walk('.'):

for name in files:

if (name == '.classpath'):

classpathFile = os.path.join(root, name)

print('Patching file:' + classpathFile)

classpathDOM = parse(classpathFile)

classPathEntries = classpathDOM.getElementsByTagName('classpathentry')

for classPathEntry in classPathEntries:

if classPathEntry.attributes["path"].value.startswith('target/generated-sources'):

# ensure that the tag exists

attributesNode = None;

for attributes in classPathEntry.childNodes:

if (attributes.nodeName == 'attributes'):

attributesNode = attributes

if (attributesNode == None):

attributesNode = classpathDOM.createElement('attributes')

classPathEntry.appendChild(attributesNode)

# search if the 'ignore_optional_problems' entry exists

hasBeenSet = 0

for node in attributesNode.childNodes:

if (node.nodeName == 'attribute' and node.getAttribute('name') == 'ignore_optional_problems'):

# it exists, make sure its value is true

node.setAttribute('value','true')

#print(node.getAttribute('name'))

hasBeenSet = 1

if (not(hasBeenSet)):

# it does not exist, add it

x = classpathDOM.createElement("attribute")

x.setAttribute('name','ignore_optional_problems')

x.setAttribute('value','true')

attributesNode.appendChild(x)

try:

f = open(classpathFile, "w")

classpathDOM.writexml(f)

print('Writing file:' + classpathFile)

finally:

f.close()

print('Done.')

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

动态编译 Java 代码可以使用 Java Compiler API,它提供了一系列的类和接口,可以在 Java 应用程序中动态编译 Java 代码。下面是一个简单的示例: ```java import javax.tools.JavaCompiler; import javax.tools.ToolProvider; public class CompilerExample { public static void main(String[] args) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int result = compiler.run(null, null, null, "Path/To/Your/Java/File.java"); if (result == 0) { System.out.println("Compilation successful"); } else { System.out.println("Compilation failed"); } } } ``` 上述代码中,我们首先通过 `ToolProvider.getSystemJavaCompiler()` 获取 JavaCompiler 对象,然后调用 `compiler.run()` 方法来编译 Java 代码。最后判断编译结果,如果返回值为 0,则表示编译成功。 接下来是生成 Jar 文件的示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; public class JarExample { public static void main(String[] args) { String[] files = {"Path/To/Your/Java/File.class"}; try { JarOutputStream jos = new JarOutputStream(new FileOutputStream(new File("Path/To/Your/Jar/File.jar"))); for (String file : files) { JarEntry entry = new JarEntry(new File(file).getName()); jos.putNextEntry(entry); jos.write(readFile(file)); jos.closeEntry(); } jos.close(); System.out.println("Jar file created successfully"); } catch (IOException e) { e.printStackTrace(); } } private static byte[] readFile(String file) throws IOException { FileInputStream fis = new FileInputStream(file); byte[] data = new byte[fis.available()]; fis.read(data); fis.close(); return data; } } ``` 上述代码中,我们首先指定需要打包的文件路径,然后使用 JarOutputStream 创建 Jar 文件并写入文件。最后,关闭 JarOutputStream,Jar 文件生成了。 需要注意的是,这里只是一个简单的示例,实际应用中可能需要更加复杂的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值