Throoze
Throoze

Reputation: 4038

How to compile a Netbeans project using javac

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.

Upvotes: 2

Views: 5771

Answers (2)

Ryan Stewart
Ryan Stewart

Reputation: 128849

In a clean temporary directory, try this sequence of commands. Then inspect and understand the directory layout, package declarations, and various path settings like -d bin, -s src, and -cp bin:

mkdir -p src/mainPackage
echo "package mainPackage; public class MyMain { public static void main(String[] args) { System.out.println(\"Hello world\"); }}" > src/mainPackage/MyMain.java
mkdir bin
javac -d bin -s src src/mainPackage/MyMain.java 
java -cp bin mainPackage.MyMain

Upvotes: 3

SHiRKiT
SHiRKiT

Reputation: 1022

Try this:

javac.exe -d class -s src src/myproject/mainpackage/*.java 
      src/myproject/mainpackage/package2/*.java 
        src/myproject/mainpackage/package3/*.java

Could try this also (Windows)

for /r %%a in (*.java) do ( javac "%%a" )

Upvotes: 0

Related Questions