java 带有 Jar 和包依赖项的 Makefile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5487838/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:21:12  来源:igfitidea点击:

Makefile with Jar and Package Dependencies

javamakefiledependenciespackagesjavac

提问by feldoh

I have been trying all day to get this thing to work. Basically I need to create a makefile to build my project from its sources. I know a little about Linux but not much and I am a complete makefile newbie.

我一整天都在努力让这件事发挥作用。基本上我需要创建一个 makefile 来从它的源代码构建我的项目。我对 Linux 有一点了解,但并不多,而且我是一个完整的 makefile 新手。

I have tried loads of examples from all over the net and all complain that they are missing dependencies. Others have suggested using Ant or Mavern but this is not possible the precise handin notice is:

我已经尝试了来自网络的大量示例,但都抱怨它们缺少依赖项。其他人建议使用 Ant 或 Mavern,但这是不可能的,确切的交接通知是:

Quoted from Specification

Your submission should consist of a single file comp2010.tar suitable for use on a Linux system. This must contain a file Makefile, and all sources. You should not hand in any .class files. Your Makefile must build the tool from the Java sources. The main class must be called Blaise. In short, the (automatic) testing process will consist of:

tar xf comp2010.tar

make

java Blaise < test.file > testout 2> testerr

These commands will be executed in an empty directory in a standard departmental teaching Linux environment with Java 1.6. The CLASSPATH will include the ANTLR jar, version 3.2.

NOTE: Please ensure that your submission can be compiled and executed on standard departmental machines. Please make sure that you use the right version of Java, and that you do not use absolute paths. If you use any external libraries, you need to submit these as well.

引用自规范

您提交的文件应包含适合在 Linux 系统上使用的单个文件 comp2010.tar。这必须包含一个文件 Makefile 和所有源。您不应提交任何 .class 文件。您的 Makefile 必须从 Java 源代码构建该工具。主类必须称为 Blaise。简而言之,(自动)测试过程将包括:

tar xf comp2010.tar

make

java Blaise < test.file > testout 2> testerr

这些命令将在 Java 1.6 的标准部门教学 Linux 环境中的空目录中执行。CLASSPATH 将包括 ANTLR jar,版本 3.2。

注意:请确保您的提交可以在标准部门机器上编译和执行。请确保您使用正确的 Java 版本,并且不使用绝对路径。如果您使用任何外部库,则还需要提交这些库。

So you see I cannot set up any environment variables as the machine to run it is not mine and I am not allowed administrative access. I cannot hand in any class files so the makefile is not for me and Ant / Mavern scripts will not work because the testing procedure is automated and uses this makefile and all I am allowed to hand in is .java files. So I need to build a makefile there is no way around that.

所以你看我不能设置任何环境变量,因为运行它的机器不是我的,我没有管理访问权限。我无法提交任何类文件,因此生成文件不适合我,并且 Ant/Mavern 脚本将无法工作,因为测试过程是自动化的并使用此生成文件,而我只能提交 .java 文件。所以我需要构建一个makefile,这是没有办法的。

The source structure is as follows:

源码结构如下:

src\Package1*.java

src\Package2*.java

auto-generated\PackageA*.java

src\Package1*.java

src\Package2*.java

自动生成\PackageA*.java

There are source files in all 3 folders needed for the thing to compile. The Main() method is in src\Package1\1.java

编译事物所需的所有 3 个文件夹中都有源文件。Main() 方法在 src\Package1\1.java

Each of these directories is a package in Eclipse and these 3 packages depend on each other as well as an external Jar file antlr-3.2.jar

这些目录中的每一个都是 Eclipse 中的一个包,这 3 个包相互依赖以及一个外部 Jar 文件 antlr-3.2.jar



So How do I make this makefile. That is my question and I have provided my own attempt below:

那么我如何制作这个makefile。这是我的问题,我在下面提供了我自己的尝试:



JAVAC = javac
CLASS_FILES = src/package1/1.class auto-generated/packageA/2.class auto-generated/packageA/3.class auto-generated/packageA/4.class src/Package2/5.class src/Package2/6.class src/Package2/7.class src/Package2/8.class src/Package2/9.class src/Package2/10.class src/Package2/11.class src/Package2/12.class antlr-3.2.jar.*

Default: $(CLASS_FILES)

%.class: %.java
    $(JAVAC) $<

clean: $(RM) *.class


This fails with errors like "org.antlr.runtime does not exist" this is inside the antlr-3.2.jar. I am at my wits end and need to hand in soon. I assume I am simply importing the jar all wrong and maybe I need to use whatever CLASSPATH is. Im sorry if this is a simple question but I have been trying for 6 solid hours now to make one of these. Any help you could give would be most appreciated.

这会失败并出现诸如“org.antlr.runtime 不存在”之类的错误,这是在 antlr-3.2.jar 中。我不知所措,需要尽快提交。我假设我只是导入了 jar 完全错误,也许我需要使用 CLASSPATH 是什么。如果这是一个简单的问题,我很抱歉,但我现在已经尝试了 6 个小时来制作其中一个。您能提供的任何帮助将不胜感激。

Kind Regards Feldoh

亲切的问候费尔多

采纳答案by rsp

I can see a few issues here:

我可以在这里看到一些问题:

  • as described you need to setup the Blaise class in the default package in the root directory of the tar file
  • any packages you use, should also be present directly under the root directory, not in a source sub-directory (as Java/Javac will not be able to find them there)
  • make is specific wrt where you use space and tab characters
  • your makefile does not use options to the javac command
  • which version of make are you trying to build a makefile for? the version I know would take a format like:

    JAVAC = javac
    JAVACFLAGS =
    SRC= Blaise.java \
        package1/1.java \
        packageA/2.java
    CLS= $(SRC:.java=.class)
    
    all:  $(CLS)
    
    
    .SUFFIXES : .class .java
    .java.class :
        $(JAVAC) $(JAVACFLAGS) $<
    
  • 如上所述,您需要在 tar 文件根目录的默认包中设置 Blaise 类
  • 您使用的任何包也应该直接存在于根目录下,而不是源子目录中(因为 Java/Javac 将无法在那里找到它们)
  • make 是您使用空格和制表符的特定位置
  • 您的 makefile 不使用 javac 命令的选项
  • 您要为哪个版本的 make 构建 makefile?我知道的版本将采用如下格式:

    JAVAC = javac
    JAVACFLAGS =
    SRC= Blaise.java \
        package1/1.java \
        packageA/2.java
    CLS= $(SRC:.java=.class)
    
    all:  $(CLS)
    
    
    .SUFFIXES : .class .java
    .java.class :
        $(JAVAC) $(JAVACFLAGS) $<
    

回答by Pa?lo Ebermann

If you use a classpath which does not include the current directory, make sure you indicate a source path, too.

如果您使用不包含当前目录的类路径,请确保您也指明了源路径。

JAVAC = javac
CLASS_FILES = src/package1/1.class auto-generated/packageA/2.class auto-generated/packageA/3.class auto-generated/packageA/4.class src/Package2/5.class src/Package2/6.class src/Package2/7.class src/Package2/8.class src/Package2/9.class src/Package2/10.class src/Package2/11.class src/Package2/12.class

CLASSPATH = antlr-3.2.jar
SOURCEPATH = .

Default: $(CLASS_FILES)

%.class: %.java
    $(JAVAC) -classpath $(CLASSPATH) -sourcepath $(SOURCEPATH) $<

clean: $(RM) *.class

The alternative would be using a classpath which includes the current directory (since the classpath is used, when you give no sourcepath):

另一种方法是使用包含当前目录的类路径(因为使用了类路径,当您不提供源路径时):

CLASSPATH = .:antlr-3.2.jar

%.class: %.java
    $(JAVAC) -classpath $(CLASSPATH) $<

(the rest as above)

(其余同上)

Of course, you could add the options to the JAVACvariable, too.

当然,您也可以将选项添加到JAVAC变量中。

回答by whatnick

Fix up your javac call by adding antlr to the classpath. I really despised the automated marking scripts my CS professors used. They stochastically failed based on submission load. Hereis a nice intro to Java and Makefiles. Basically try to glob your .java and .class files. Use the JFLAGS to pass antlr in the classpath.

通过将 antlr 添加到类路径来修复您的 javac 调用。我真的很鄙视我的 CS 教授使用的自动评分脚本。他们根据提交负载随机失败。是 Java 和 Makefiles 的一个很好的介绍。基本上尝试 glob 你的 .java 和 .class 文件。使用 JFLAGS 在类路径中传递 antlr 。