I'm writing a project in netbeans, but I need it to be compiled with javac, because it is needed to be built and executed through CLI. How can I do that? I've seen a lot of project specific answers, but none suits my problem. I've read javac's man, and setted the classpath properly: javac -cp .. MyMain.java, conssidering that my project have the following structure:
src
|_mainPackage
|_package2
|_package3
|_MyMain.java
and I'm using the dot format in my imports like this:
import package1.class1
import package2.class2
public class MyMain {
public static void main(String[] args) {
//Stuff with classes in package1
.
.
.
//Stuff with classes in package2
.
.
.
}
}
I can't use ant, because it is meant to be compiled using javac in the CLI, since it is a college project and have some restrictions imposed by the professors.
EDIT:
I actually got to compile my sources using javac with the command usr@host: mainPackage $ javac -cp .. MyMain.java, since my imports use dot separation format, and the "root package" is located in 'mainPackage' folder; but when i'm going to run the project, i have this output:
usr@host: mainPackage $ java VideoRent
Exception in thread "main" java.lang.NoClassDefFoundError: MyMain (wrong name: mymain/MyMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: MyMain. Program will exit.
cdup to the folder representing the package root or something? Or don't you know how to compile multiple classes altogether? Are you able to compile a single class?javac MainClas.javato compile, and thenjava MainClassto run; and it handled every dependency I needed. Actually, using the command above, i made it to compile my class, but then i'm getting a weird error. check my edit.java MyMainwhile the class itself is in themymainpackage and should thus be executed asjava mymain.MyMain. But this doesn't correspond with the file structure and code examples you've given as far. This means that you've obfuscated/oversimplified the code too much without realizing what you were doing. In the future please rename package/classnames for in the question with extreme care, or at least setup a playground project.