简体   繁体   English

Java:设置类路径

[英]Java: Setting Classpath

I have a project due soon and everything is coming together really nicely, but java classpaths are getting seriously in the way. 我有一个即将到期的项目,而且一切都进展顺利,但是java类路径正在变得越来越严重。 I'll try to explain the situation as clearly and thoroughly as I can. 我将尽我所能尽可能清楚地解释这种情况。

So we are using Javacc to write a programming language. 因此,我们使用Javacc编写一种编程语言。 The javacc file compiles into several java files. javacc文件编译成几个java文件。 The Parser.java file includes references and calls to the other generated java files. Parser.java文件包括对其他生成的Java文件的引用和调用。 So, after generation, we compile the Parser.java file. 因此,生成后,我们将编译Parser.java文件。 The problem is we get many errors, including not being able to recognize the calls to the other java files as well as our own files. 问题是我们遇到了很多错误,包括无法识别对其他java文件以及我们自己的文件的调用。 We asked on a classroom discussion board about our problem and the professor responded with "you need to have the class files in your classpath". 我们在教室讨论板上询问了我们的问题,然后教授回答“您需要在课程路径中包含课程文件”。 Ok, great, so the question is, how do we do that? 好的,很好,问题是,我们该怎么做? Basically we have a single directory with the generated java files and our other helper files. 基本上,我们只有一个目录,其中包含生成的Java文件和其他帮助文件。

So, what have we tried? 那么,我们尝试了什么?

I have tried changing my .bashrc (Ubuntu) file to include the correct classpath but that doesn't work. 我尝试更改.bashrc(Ubuntu)文件以包括正确的类路径,但这不起作用。 ie

 CLASSPATH=Home/project

(something like that I had the syntax right in the file) (就像我在文件中使用的语法一样)

I've tried on compilation executing 我试过编译执行

javac -cp . Parser.java

and

javac -cp "." Parser.java

neither works. 都不起作用。

I have tried to edit the xml (I think xml) .classpath file in the directory of our files. 我试图在文件目录中编辑xml(我认为是xml).classpath文件。 Still doesn't work. 仍然不起作用。

Somehow, I was able to compile Parser.java in one of the directories I have (we ended up making multiple directories with the same files in it in a futile effort to make something work) but when I try to run 不知何故,我能够在我拥有的目录之一中编译Parser.java(我们最终创建了多个目录,其中包含相同的文件,这是徒劳的努力,以使某些工作正常进行),但是当我尝试运行时

java -cp . Parser.java

or 要么

java Parser.java

It says it can not find the main and throws (I believe, its on my other computer) ClassNotFound or ClassNotDefined exception (something like that, it cannot find the main in the Parser file eventhough it IS there). 它说找不到主程序,并抛出(我相信,在我的另一台计算机上)它的ClassNotFound或ClassNotDefined异常(类似的东西,即使它存在,它也无法在Parser文件中找到主程序)。

We have tried adding packages deceleration and import statements to our file, nothing seems to work. 我们尝试将软件包减速和导入语句添加到我们的文件中,似乎没有任何效果。

BASICALLY: How can I successfully change the Classpath so that my java files (all in one directory and not jarred) can be compiled and run on my machine? 基本上:如何才能成功更改类路径,以便可以在我的计算机上编译并运行我的java文件(全部位于一个目录中,而不是受jared影响)?

Thank you for your help. 谢谢您的帮助。 I greatly appreciate it. 我非常感谢。

I highly suggest you look into Ant . 我强烈建议您研究Ant A simple build file can solve all these problems for you. 一个简单的构建文件可以为您解决所有这些问题。

You don't need to edit your .bashrc or CLASSPATH . 您不需要编辑.bashrcCLASSPATH

From the command line you need to build ALL the java files together. 从命令行,您需要一起构建所有Java文件。 I am not sure if JavaCC needs javacc.jar after it's generated your Lexer and Parser. 我不确定在生成javacc.jar和Parser后JavaCC是否需要javacc.jar But let's assume it does for some generic AST support. 但让我们假设它确实对某些通用AST支持有效。

javacc.jar is located in ~/javacc-5.0/lib/javacc.jar javacc.jar位于~/javacc-5.0/lib/javacc.jar

Scenario 1: Simple directory structure, all Java files are in the root folder with no package. 方案1:目录结构简单,所有Java文件都位于根目录中,没有包。

root
  | Parser.java
  | Lexer.java
  | Program.java

To compile these I need to run: 要编译这些文件,我需要运行:

javac -cp ~/javacc-5.0/lib/javacc.jar Parser.java Lexer.java Program.java

then I can execute Program like so if Program has main 那么我可以像这样执行Program ,如果程序具有main

java -cp ~/javacc-5.0/lib/javacc.jar:. Program

Scenario 2: Medium directory structure, code in root but with packages. 场景2:中等目录结构,代码在根目录下,但带有软件包。

root
  | org
     | myproject
          | Parser.java
          | Lexer.java
          | Program.java

then you need to execute javac like so: 那么您需要像这样执行javac:

javac -cp ~/javacc-5.0/lib/javacc.jar org/myproject/Parser.java org/myproject/Lexer.java org/myproject/Program.java

and to execute 并执行

java -cp ~/javacc-5.0/lib/javacc.jar:. org.myproject.Program

Scenario 3: Complex directory structure, specific source directory with packages. 方案3:复杂的目录结构,带有包的特定源目录。

root
   | src
      | org
          | myproject
              | Parser.java
              | Lexer.java
              | Program.java

then you need to execute javac like so: 那么您需要像这样执行javac:

javac -cp ~/javacc-5.0/lib/javacc.jar -sourcepath src src/org/myproject/Parser.java src/org/myproject/Lexer.java src/org/myproject/Program.java

and to execute 并执行

java -cp ~/javacc-5.0/lib/javacc.jar:src org.myproject.Program

There is a difference between javac and java . javacjava之间有区别。 You try to use them in wrong way. 您尝试以错误的方式使用它们。 javac is a compiler which produces *.class files (in your case Parser.class). javac是一个生成* .class文件的编译器(在您的情况下为Parser.class)。 This *.class file must be then run by java . 然后必须由java运行此* .class文件。

So if I have file Parser.java 所以如果我有Parser.java文件

class Parser {

  public static void main(String[] args) {
    System.out.println("Hi");
  }

}

I would go the directory where this files resides and run in shell: 我将转到此文件所在的目录并在shell中运行:

javac Parser.java

Than run the compiled file 比运行编译文件

java Parser

And that's it. 就是这样。 Note that the name of the class in java file must be same as the name of the file. 请注意,java文件中类的名称必须与文件名相同。

  1. Create a Jar file of all your compiled classes. 创建所有已编译类的Jar文件。 Refer to this tutorial on how to create a jar file. 请参考本教程 ,了解如何创建jar文件。

  2. Start you program with this command 使用此命令开始编程

      java -classpath pathToJarFile com.abc.MainClass 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM