2

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.
5
  • Just the same way as you compile multiple classes. What exactly is the problem you're having? Don't you know how to cd up 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? Commented Oct 6, 2011 at 2:56
  • It looks like you are setting your CLASSPATH to one directory up from your current directory "javac -cp .. MyMain.java" which could be causing your problem. Commented Oct 6, 2011 at 3:00
  • @BalusC: well, I have a long time since I last programmed in java, but I remember that I didnt used packages at all. I put all my classes under the same folder, and executed javac MainClas.java to compile, and then java MainClass to 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. Commented Oct 6, 2011 at 7:45
  • 2
    You don't have compile problems at all. You have execution problems. The exception is telling that you executed the class as java MyMain while the class itself is in the mymain package and should thus be executed as java 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. Commented Oct 6, 2011 at 11:15
  • @BalusC: yes! when i first posted the question, i couldn't even compile, but by the time i edited, i could compile but not execute. That's the answer! using dot notation on java call. Just like you pointed, i now realize i was wrong in package structure... anyways, thank you very much for your efforts! Commented Oct 6, 2011 at 18:16

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

0

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" )

2 Comments

thank you very much!, but im using linux, and my project will be executed on linux Debian. Ive read javac's man, and says the -d option is the directory where .class files are going to be stored, so i cant pass a class to -d option. Any ideas on linux?
-d class means to put the .class files in folder called class ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.