将具有两个类的单个.java文件编译为两个.class文件 [英] Compile single .java file with two classes into two .class files

查看:272
本文介绍了将具有两个类的单个.java文件编译为两个.class文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个.java文件设置如下:

I currently have a .java file set up like this:

package com.ds;

class c{...}

public class Main{...}

当我编译文件Main.java时,它会产生一个单独的.class文件Main.class。

When I compile the file Main.java, it results in a single .class file being Main.class.

运行.class与 java com.ds.Main 它不工作!

When I try to run the .class with java com.ds.Main it does not work! It says it cannot find or load the class.

当我尝试使用 java Main 运行.class时

When I try to run the .class with java Main it runs, but I get an error like so:

Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: com
/DatingService/Main)


at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

在我试图找到一个解决方案之前,没有找到的解决方案应用于我或只是没有工作。

I've seen this before while trying to find a solution and none of the solutions I found applied to me or just didn't work.

在做了一些更多的研究后,我假设javac不会拆分文件中的类至少默认?我知道许多IDE,如Eclipse和IntelliJ将类分成两个单独的.class文件(我可以确认这一点)。所以有什么方法javac这样做吗?我使用javac作为IntelliJ的编译器,所以一定有一种方法,除非它在编译之前完成。

After doing a bit more research I am assuming that javac will not split the classes within a file by at least default? I know that many IDEs like Eclipse and IntelliJ split the classes into two separate .class files (I can confirm this). So is there any way for javac to do this? I'm using javac as my compiler for IntelliJ so there must be a way unless it's done before compiling.

如果我删除包,我可以运行java Main只有一个.class文件被编译。所以我有点困惑,有点绝望。我试图完全避免更改我的代码或将类拆分成两个单独的.java文件。

If I remove the package, I can run java Main perfectly fine with only a single .class file compiled. So I'm a bit confused, and a little desperate. I am trying to completely avoid changing my code or splitting the classes into two separate .java files.

推荐答案

假设您有目录和文件

[myProject]
  |
  +--[src]
  |    |
  |    +--[com]
  |        |
  |        +--[DatingService]
  |            |
  |            +-- Main.java
  |
  +--[classes]

您的Main.java文件看起来像



and your Main.java file looks something like

package com.DatingService;

class c{
    private int i;
    public void setI(int i){
        this.i=i;
    }
    public int getI(){
        return this.i;
    }
}

public class Main{
    public static void main(String[] args){
        c myCVariable = new c();

        myCVariable.setI(10);
        System.out.println(myCVariable.getI());
    }
}

在终端中,您需要转到 myProject 目录,并从中使用

In terminal you need to go to myProject directory and from it use

myProject> javac -d classes src\com\DatingService\\ \\ Main.java

myProject>javac -d classes src\com\DatingService\Main.java

感谢 -d 编译的类应该放在 classes 目录中(注意 classes 目录必须已经存在)。因此 c.class Main.class 将放置在 myProject\classes \ com \DatingService

Thanks to -d (directory) parameter packages with all compiled classes should be placed in classes directory (note that classes directory must already exist). So c.class and Main.class will be placed in myProject\classes\com\DatingService.

现在运行 main c> Main 类只需要提供有关包含你的包的目录的信息(这是ClassPath),并使用 full.package.name.to.your.MainClass 。因此,您必须向中添加 -classpath类(或更短的 -cp类)参数> java 命令并运行它

Now to run main method from Main class you just need to provide information about directory that contains your packages (this is ClassPath) and also use full.package.name.to.your.MainClass. So you will have to add -classpath classes (or shorter -cp classes) parameter to your java command and run it like

myProject> java -cp classes com.DatingService.Main

myProject>java -cp classes com.DatingService.Main

(注意:后面没有 .java 因为JVM正在运行存储在 .class 文件中的二进制文件,而不是来自 .java 文件的代码)

(note: there is no .java suffix after Main class since JVM is running binaries stored in .class files, not code from .java files)

这篇关于将具有两个类的单个.java文件编译为两个.class文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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