为什么这个javac:File Not Found错误? [英] Why this javac: File Not Found error?

查看:1104
本文介绍了为什么这个javac:File Not Found错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CentOS 5 linux上使用一些基本的java应用程序,我将 classpath 设置为指向 home / pathToJava / bin 其中包含 javac java



我在中有 .java 文件home / pathToFolderA / src



<当我运行 javac

时,

home / pathToFolderB / gen-java



home / pathToFolderA / src 一切正常工作



但是当我在中运行 javac 时,home / pathToFolderB / gen-java c $ c> fileName.java 我得到一个文件未找到错误,特别是

  javac:file找不到:fileName.java 
用法:javac< options> <源文件>

为什么会发生这种情况?



感谢所有帮助

解决方案

您不应将类路径设置为指向您的JDK bin目录 - 而是应该是 PATH 环境变量,它为类路径提供不同的目的。 (类路径定义了包含编译的Java .class代码的jar和目录的列表; PATH变量定义了一个路径列表,当shell在当前目录中找不到时,shell需要查找和定位要执行的程序,所以如果你类型例如 zip - 它将查看 PATH 中定义的所有目录,并确定 zip 程序位于 / usr / bin
其次,如果要从两个目录编译源代码, :


  • 所有源的路径( home / pathToFolderA / src home / pathToFolderB / gen-java

  • 要生成编译的.class文件的路径

  • 在类路径中指定您可能在源文件中使用的任何库



像这样编译:

  javac -d / home / pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/到/ another.jar home / pathToFolderA / src / *。java home / pathToFolderB / gen-java / *。java 


$ b b

并运行您编译的程序:

  java -classpath /path/to/some.jar:/path/到/ another.jar:/ home / pathToFolderWithResultsOfCompilation full.name.of.your.Java 


Working with some basic java apps on CentOS 5 linux and I have my classpath set to point to home/pathToJava/bin which contains javac and java

and I have .java files in home/pathToFolderA/src

and home/pathToFolderB/gen-java

When I run javac and java in home/pathToFolderA/src everything works perfectly

But when I run javac from within home/pathToFolderB/gen-java on fileName.java I get a file not found error, specifically

javac: file Not found: fileName.java
Usage: javac <options> <source files>

Why could this be happening?

Thanks for all help

解决方案

You shouldn't set your classpath to point to your JDK bin directory -- instead it should be the PATH environment variable, which serves a different purpose to classpath. (The classpath defines a list of jars and directories containing compiled Java .class code; the PATH variable defines a list of paths where the shell needs to look and locate programs to execute when they are not found in the current directory -- so if you type for instance zip -- it would look in all the directories defined in PATH and figure out that zip program is located under /usr/bin) Secondly if you want to compile sources from both directory you need to specify:

  • all the paths where the sources are (both home/pathToFolderA/src and home/pathToFolderB/gen-java)
  • the path where the compiled .class files to be generated
  • specify in the classpath any library you might use in your source files

To sum it up, it would be something like this to compile:

javac -d /home/pathToFolderWithResultsOfCompilation -classpath /path/to/some.jar:/path/to/another.jar home/pathToFolderA/src/*.java home/pathToFolderB/gen-java/*.java 

and to run your compiled programs:

java -classpath /path/to/some.jar:/path/to/another.jar:/home/pathToFolderWithResultsOfCompilation full.name.of.your.Java

这篇关于为什么这个javac:File Not Found错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆