简体   繁体   English

javac将文件从文件夹A编译到文件夹B(linux)

[英]javac compile files from folder A to folder B ( linux )

I have a small java project. 我有一个小的java项目。 And i want to keep a structure in how the files are organized in my project folder. 我想在我的项目文件夹中保留文件组织结构。

There are 3 folders: bin/ , res/ and src/ . 有3个文件夹: bin/res/src/ In src are all source files. 在src中都是源文件。 In res are some resources like textfiles to be printed are files that are saved during runtime. 在res中,一些资源(如要打印的文本文件)是在运行时保存的文件。 And in bin is where all the binarys should be. 在垃圾箱里所有的二进制文件应该是。

I found out, that i can compile my sourcefiles with the -d argument to the bin folder. 我发现,我可以使用-d参数将我的源文件编译到bin文件夹。

javac -d bin src/*.java

But I can't run java bin/Main from my project folder it gives me a classnotfound error. 但我不能从我的项目文件夹中运行java bin/Main它给我一个classnotfound错误。 (Going in bin and then run java Main works. why??) (进入bin然后运行java Main works。为什么?)

Second problem is, that res files are only accessible when res/ is in bin/ . 第二个问题是,仅当res/bin/时才能访问res文件。 I want it kind of like in Eclipse. 我希望它有点像Eclipse。 In the sourcecode files in res/ are used like the executor is in the project folder. res/中使用的源代码文件中,执行程序位于项目文件夹中。

I hope you understand what i'm trying to do. 我希望你明白我想做什么。 And thanks for any help! 并感谢您的帮助!

Java requires the .class files to be locatable from the classpath root directory. Java要求.class文件可以从类路径根目录中找到。 So if your classpath is . 所以如果你的类路径是. , then the class com.mycompany.MyClass should be at ./com/mycompany/MyClass.class . ,然后类com.mycompany.MyClass应该在./com/mycompany/MyClass.class So to run your files from your base directory, set the classpath by giving the -cp bin command-line argument to java . 因此,要从基本目录运行文件,请通过向java提供-cp bin命令行参数来设置类路径。

How are you trying to get to the res files? 您是如何尝试访问res文件的? If you use the -cp trick above, you should be fine with something like new FileReader("res/file1.txt") . 如果您使用上面的-cp技巧,您应该可以使用new FileReader("res/file1.txt") If you run in the bin folder, you would need new FileReader("../res/file1.txt") 如果你在bin文件夹中运行,你需要new FileReader("../res/file1.txt")

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

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