Compile single .java file with two classes into two .class files -
i have .java file set this:
package com.ds; class c{...} public class main{...}
when compile file main.java, results in single .class file being main.class.
when try run .class java com.ds.main
not work! says cannot find or load class.
when try run .class java main
runs, error so:
exception in thread "main" java.lang.noclassdeffounderror: main (wrong name: com /datingservice/main) @ java.lang.classloader.defineclass1(native method) @ java.lang.classloader.defineclass(classloader.java:792) @ java.security.secureclassloader.defineclass(secureclassloader.java:142) @ java.net.urlclassloader.defineclass(urlclassloader.java:449) @ java.net.urlclassloader.access$100(urlclassloader.java:71) @ java.net.urlclassloader$1.run(urlclassloader.java:361) @ java.net.urlclassloader$1.run(urlclassloader.java:355) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(urlclassloader.java:354) @ java.lang.classloader.loadclass(classloader.java:424) @ sun.misc.launcher$appclassloader.loadclass(launcher.java:308) @ java.lang.classloader.loadclass(classloader.java:357) @ sun.launcher.launcherhelper.checkandloadmain(launcherhelper.java:482)
i've seen before while trying find solution , none of solutions found applied me or didn't work.
after doing bit more research assuming javac not split classes within file @ least default? know many ides eclipse , intellij split classes 2 separate .class files (i can confirm this). there way javac this? i'm using javac compiler intellij there must way unless it's done before compiling.
if remove package, can run java main fine single .class file compiled. i'm bit confused, , little desperate. trying avoid changing code or splitting classes 2 separate .java files.
i not sure doing wrong show how can done.
lets have directories , files
[myproject] | +--[src] | | | +--[com] | | | +--[datingservice] | | | +-- main.java | +--[classes]
and main.java file looks 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()); } }
in terminal need go myproject
directory , use
myproject>javac -d classes src\com\datingservice\main.java
thanks -d
(directory) parameter packages compiled classes should placed in classes
directory (note classes
directory must exist). c.class
, main.class
placed in myproject\classes\com\datingservice
.
now run main
method main
class need provide information directory contains packages (this classpath) , use full.package.name.to.your.mainclass
. have add -classpath classes
(or shorter -cp classes
) parameter java
command , run like
myproject>java -cp classes com.datingservice.main
(note: there no .java
suffix after main
class since jvm running binaries stored in .class
files, not code .java
files)
Comments
Post a Comment