5 Replies - 402 Views - Last Post: 06 October 2013 - 02:27 PM Rate Topic: -----

#1 Aaron7pm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-August 12

Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 11:58 AM

I'm in a bit of an urgent predicament.

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

package com.ds;

class c{...}

public class Main{...}


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

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.

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.

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.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Compile single .java file with two classes into two .class files

#2 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 12:46 PM

I'm not 100% sure but based on the error message, it looks like you created a directory named "Dating Service" within "com" but the compiler appears to be looking for your Main class in a package named com.ds

Also, you probably do not want to name your class Main since there is already a method named main() :)
Was This Post Helpful? 0
  • +
  • -

#3 Aaron7pm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-August 12

Re: Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 12:50 PM

View Postjasonjh, on 06 October 2013 - 12:46 PM, said:

I'm not 100% sure but based on the error message, it looks like you created a directory named "Dating Service" within "com" but the compiler appears to be looking for your Main class in a package named com.ds

Also, you probably do not want to name your class Main since there is already a method named main() :)/>

com.ds is just the shorthand for the example. The full package name is com.DatingService.

The Main class was IDE generated, just haven't bothered to change it as it makes no real difference.
Was This Post Helpful? 0
  • +
  • -

#4 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 12:55 PM

Quote

com.ds is just the shorthand for the example. The full package name is com.DatingService.

The Main class was IDE generated, just haven't bothered to change it as it makes no real difference.

Oh ok. I understand. At any rate, I'm not familiar with IntelliJ IDE but I'm sure one of the other members will be able to help you :)
Was This Post Helpful? 0
  • +
  • -

#5 Aaron7pm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-August 12

Re: Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 01:08 PM

I was able to get a solution on another site, basically the solution is just specifying output directory and classpath.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2007
  • View blog
  • Posts: 4,172
  • Joined: 11-December 07

Re: Compile single .java file with two classes into two .class files

Posted 06 October 2013 - 02:27 PM

Any chance you could link us to the solution?

I wrote the following a while ago but the site went down before I could post it:

Javac doesn't put classes into separate files, the programmer does.

Strictly speaking, each java file can contain at most exactly one public class, named the same as the filename. So if your public class is Main then the filename must be Main.java. You may also have non-public classes but that's usually a bad idea. To that end,I recommend you put your c class in its own file and rename both to have a descriptive name.

The other thing to watch out for is that the directory structure must mirror the package. So, the class com.ds.Main must be in a directory ./com/ds/Main.java and com.cd.c must be in ./com/ds/c.java.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1