Darth Shirr
Darth Shirr

Reputation: 587

Basic Java - Packaging

I decided to post this question after trying to find an answer for it, and couldn't find one. I'm studying for OCJP and tried few simple codes. This is what I did and need to do.

  1. Created two .java sources, say TestOne.java, TestTwo.java [using notepad++]
  2. Created a directory named "package1" and placed the two sources in them.
  3. Both the source files have "package package1;" as their first statement.
  4. TestOne.java has one public class and one class with default access.
  5. TestTwo.java has one default class with an object of the default class in TestOne.java.
  6. The main method is in this default class in TestTwo.java. It tries to invoke a method in the referred object that was created, using TestOne.java default class.

So after all that was set up compiled TestOne.java then TestTwo.java by setting the flag "classpath" in javac [ javac -classpath ]. Complied. But when I tried to run it gave me an exception "Exception in thread "main" java.lang.NoClassDefFoundError". Does anyone know what's wrong ?

Upvotes: 0

Views: 100

Answers (2)

a dawg
a dawg

Reputation: 69

try this

javac -d path cname.java

so write the code like this

javac -d c:\main testone.java javac -d c:\main testtwo.java

c:\main should exist in your pc

then while executing

java -cp path pn.classname

so whichever class contains main (say test2)

java -cp c:\main package1.testtwo

Upvotes: 0

Nambi
Nambi

Reputation: 12042

run the code after compile

compile javac TestTwo.java

run after compile java TestTwo

Upvotes: 1

Related Questions