使用 javac/java 手动编译和运行多个包 [英] Manually compile and run multiple packages using javac/java

查看:43
本文介绍了使用 javac/java 手动编译和运行多个包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下文件夹结构:

Let's say I have the following folder structure:

srccomprojpp1stuff 其中 com.proj.pp1com.proj.pp1.stuff 是我拥有的两个包:

srccomprojpp1stuff where com.proj.pp1 and com.proj.pp1.stuff are the two packages I have:

    -src
      -com
        -proj
          -pp1
             tester.java
             -stuff
                some other .java's

作为 tester.java 中的主函数,并使用/stuff 中声明的类.

Being the main function in tester.java, and using the classes declared in /stuff.

tester.java 中,我以这种方式声明导入:

In tester.java I declare the imports that way:

package com.proj.pp1;    
import com.proj.pp1.*;

在所有声明的文件中:

package com.proj.pp1.stuff;

没有任何导入.

我可以让 .class 执行以下操作,位于 /pp1 文件夹中:

I'm able to get the .class's doing the following, being in /pp1 folder:

javac tester.java stuff/*.java

但是当我尝试运行测试类时:

But when I try to run the tester class:

java tester

它给了我错误输出:无法找到或加载主类测试器如果我尝试 java com.proj.pp1.tester

It gives me the error output: Could not find or load main class tester and the very same thing if I try java com.proj.pp1.tester

您认为可能存在的实际问题是什么?

What do you think could be the actual problem?

推荐答案

javacfiles 作为参数,所以应该是

javac takes files as arguments, so it should be

cd src
javac com/proj/pp1/tester.java

那应该编译 tester.java 和所有相关的类.

That should compile tester.java and all related classes.

java 将主 class 作为参数,因此要运行它,您可以:

and java takes the main class as argument, so to run it you do:

java com.proj.pp1.tester

(当您调用 java com.proj.pp1.tester 时,请确保 com/proj/pp1/tester.class 存在.)

(When you invoke java com.proj.pp1.tester make sure com/proj/pp1/tester.class exists.)

您可能希望阅读 javac-d 选项,以避免将您的类放在源目录中.

You might want to read up on -d option for javac to avoid putting your classes in the source directory.

这篇关于使用 javac/java 手动编译和运行多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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