Chapter 5-01

Please indicate the source if you want to reprint: http://blog.youkuaiyun.com/gaoxiangnumber1.
5.1 Capabilities and Limitations of Optimizing Compilers
Compilers must be careful to apply only safe optimizations to a program, meaning that the resulting program will have the exact same behavior as would an unoptimized version for all possible cases the program may encounter.
twiddle2 is more efficient: it requires only three memory references (read *xp, read *yp, write *xp), whereas twiddle1 requires six (two reads of *xp, two reads of *yp, and two writes of *xp).
When xp and yp are equal. Then function twiddle1 will perform the following computations:
The result will be that the value at xp will be increased by a factor of 4. On the other hand, function twiddle2 will perform the following computation:
The result will be that the value at xp will be increased by a factor of 3. The compiler knows nothing about how twiddle1 will be called, and so it must assume that arguments xp and yp can be equal. It therefore cannot generate code in the style of twiddle2 as an optimized version of twiddle1.(memory alias)
Code involving function calls can be optimized by a process known as inline substitution (or simply “inlining”), where the function call is replaced by the code for the body of the function.
5.2 Expressing Program Performance
We introduce the metric cycles per element(CPE) as a way to express program performance.
The sequencing of activities by a processor is controlled by a clock providing a regular signal of some frequency, usually expressed in gigahertz (GHz), billions of cycles per second. A “4 GHz” processor’s processor clock runs at 4.0 × 109 cycles per second.
The time required by such a procedure can be characterized as a constant plus a factor proportional to the number of elements processed.
We refer to the coefficients as the effective number of cycles per element(“CPE”). By this measure, psum2, with a CPE of 6.50, is superior to psum1, with a CPE of 10.0.
5.3 Program Example
5.4 Eliminating Loop Inefficiencies
5.5 Reducing Procedure Calls
5.6 Eliminating Unneeded Memory References
Please indicate the source if you want to reprint: http://blog.youkuaiyun.com/gaoxiangnumber1.

<think>好的,我现在要解决用户的问题:如何在Java项目中设置JAR文件的主清单属性以避免出现“no main manifest attribute”错误。这个问题看起来和JAR包的打包配置有关,特别是缺少主类的指定。 首先,我需要回忆一下Java打包的相关知识。JAR文件需要一个清单文件MANIFEST.MF,其中包含Main-Class属性来指定入口类。如果这个属性缺失,运行时会报错。用户提到的错误信息引用自多个来源,比如引用[2]和[3],都指出需要配置manifest或指定main class。 接下来,我需要考虑不同的构建工具,比如Maven和Gradle,因为它们的配置方式不同。用户可能使用的是Maven,因为在引用[3]中提到了maven打包的问题。同时,用户也可能使用Spring Boot,因为Spring Boot的插件处理方式不同。 对于Maven项目,通常使用maven-jar-plugin或spring-boot-maven-plugin。如果用户使用普通Maven项目,需要在pom.xml中配置maven-jar-plugin,指定Main-Class。如果使用Spring Boot,可能需要检查是否正确配置了spring-boot-maven-plugin,并且没有排除掉相关的配置,比如<skip>true</skip>,这会导致插件被跳过,进而没有生成正确的清单文件。 另外,用户可能错误地配置了其他插件,或者在构建过程中排除了主类配置。例如,引用[3]提到删除build中的<exclude>true</exclude>配置,这可能是指某些插件配置中的参数被设置为跳过主类设置,需要修正这些配置。 还需要考虑直接使用命令行打包的情况,这时候需要用jar命令手动指定主类。但大多数情况下用户会使用构建工具,所以可能这个情况不是主要问题。 现在,我需要整理出针对不同情况的具体解决步骤。首先,检查构建工具,然后根据工具配置相应的插件,确保Main-Class被正确写入MANIFEST.MF。同时,注意是否有配置错误导致插件被跳过,比如在Maven中是否有<skip>true</skip>这样的设置。 可能用户遇到的问题是在Maven打包时没有正确配置插件,或者在Spring Boot项目中遗漏了必要的插件配置。需要分步骤说明如何在这些情况下正确配置,并验证生成的JAR文件是否包含正确的主清单属性。 最后,还需要提醒用户检查生成的MANIFEST.MF文件,确认Main-Class是否正确,以及如何运行JAR文件。可能还需要解释如何解压JAR文件查看清单内容,或者使用jar命令查看。 总结一下,解决步骤大致分为:确定构建工具,配置对应插件,检查是否有干扰配置,重新打包并验证。同时给出常见构建工具的具体配置示例,如Maven和Gradle,并针对Spring Boot项目做特别说明。</think>### 如何解决Java JAR包“没有主清单属性”错误? 当Java项目打包成JAR文件时,若缺少主清单属性(`Main-Class`),运行时会提示错误:`no main manifest attribute`。以下是分步解决方法: --- #### **1. 确认主类存在** 确保项目中已定义包含`public static void main(String[] args)`方法的主类,例如: ```java public class MainApp { public static void main(String[] args) { System.out.println("Hello World"); } } ``` --- #### **2. 配置构建工具** 根据项目使用的构建工具(如Maven或Gradle),添加主类配置: ##### **Maven项目** 在`pom.xml`中配置`maven-jar-plugin`,指定主类: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainApp</mainClass> <!-- 替换为实际主类全限定名 --> </manifest> </archive> </configuration> </plugin> </plugins> </build> ``` ##### **Spring Boot项目** 使用`spring-boot-maven-plugin`,确保插件未被跳过: ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.MainApp</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> ``` **注意**:若存在`<skip>true</skip>`配置(如某些多模块项目),需删除此配置以避免跳过主类生成[^3]。 ##### **Gradle项目** 在`build.gradle`中添加: ```groovy jar { manifest { attributes 'Main-Class': 'com.example.MainApp' } } ``` --- #### **3. 重新打包并验证** - 执行打包命令(如`mvn clean package`或`gradle build`)。 - 检查生成的JAR文件中是否包含`MANIFEST.MF`文件,路径为`META-INF/MANIFEST.MF`。 - 确认清单中包含以下内容: ```manifest Main-Class: com.example.MainApp ``` --- #### **4. 常见问题排查** - **插件冲突**:多模块项目中可能因插件配置覆盖导致主类丢失,需检查父模块与子模块的配置[^2]。 - **手动打包错误**:若使用`jar`命令打包,需显式指定主类: ```bash jar cfe app.jar com.example.MainApp *.class ``` --- #### **5. 运行JAR文件** ```bash java -jar your-app.jar ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值