Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Difference of java .... and javac + java

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Difference of java .... and javac + java

    java -version
    java version "14" 2020-03-17
    Java(TM) SE Runtime Environment (build 14+36-1461)
    Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)


    package reusing;
    import static net.mindview.util.Print.*;
     
    class Cleanser {
      private String s = "Cleanser";
      public void append(String a) { s += a; }
      public void dilute() { append(" dilute()"); }
      public void apply() { append(" apply()"); }
      public void scrub() { append(" scrub()"); }
      public String toString() { return s; }
      public static void main(String[] args) {
        Cleanser x = new Cleanser();
        x.dilute(); x.apply(); x.scrub();
        print(x);
      }
    }	
     
    public class Detergent extends Cleanser {
    	private Cleanser cl = new Cleanser();
      // Change a method:
    	public void append(String a){
    		cl.append(a);
    	}
     
    	public void dilute(){
    		cl.dilute();
    	}	
     
    	public void apply(){
    		cl.apply();
    	}	
     
    	public void scrub(){
    		cl.scrub();
    	}	
     
     // Test the new class:
      public static void main(String[] args) {
        Detergent x = new Detergent();
        x.dilute();
        x.apply();
        x.scrub();
        print(x);
        print("Testing base class:");
        Cleanser.main(args);
      }	
    }

    Attention, please, to the first line (where "package reusing;");

    Then.

    Case 1.

    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ java -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples:. Detergent.java
    Cleanser dilute() apply() scrub()

    A paradox.

    Working. But I think it shouldn't.

    Case 2.

    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ javac -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples:. Detergent.java
    michael@michael:~/Downloads/thinking_in_java/TIJ4-code-master/examples/reusing$ java -cp /home/michael/Downloads/thinking_in_java/TIJ4-code-master/examples:. Detergent
    Error: Could not find or load main class Detergent
    Caused by: java.lang.NoClassDefFoundError: reusing/Detergent (wrong name: Detergent)

    Well. This is expected. If I comment out that abovementioned line, the code will work and compile.

    Could you tell me why the code worked in case 1?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difference of java .... and javac + java

    The full class name includes all package names. The full class name would be: reusing.Detergent
    The command to execute would be: java reusing.Detergent
    That command would have to be issued in the folder that contains the reusing folder, Not in the folder with the Detergent.class file
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java 2 & Java 6 Books - What's the difference?
    By Hawwi in forum Java Theory & Questions
    Replies: 6
    Last Post: December 15th, 2013, 02:45 PM
  2. I am a new Java user, I can't use "javac" in my computer
    By Anna in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 22nd, 2013, 06:18 AM
  3. The difference between two Java libraries
    By ice in forum Java Theory & Questions
    Replies: 1
    Last Post: January 11th, 2011, 10:34 AM