-1

I am stuck on a problem because I am not very good in Java. Could you help me.

PLEASE NOTE: It is not a duplicate as the classpaths are specified and I have already gone through the "duplicate question".

Could you please point me in the right direction?

My code compiles fine and all the compiled files, along with original files, are stored on Desktop.

ERROR

Desktop beeeeaasssst$ java com.StudentDemo
Error: Could not find or load main class com.StudentDemo
Caused by: java.lang.ClassNotFoundException: com.StudentDemo

CODE

    package com;

public class StudentDemo {

    public static void main(String[] args) {

        Student one = new Student(1,"Ravi",45);
        Student two = new Student(2,"Amit",65);
        Student three = new Student(3,"pooka",55);

        System.out.println("Student with highest marks is " + compareStudents(one,two,three));
    }

    public static String compareStudents (Student one, Student two, Student three) {
        Student st = one;

        if (two.getMarks() > st.getMarks())
            st=two;

        if (three.getMarks() > st.getMarks())
            st=three;

        return st.getName();
    }
}

class Student {
    private int rollNo;
    private String name;
    private double marks;

    public Student (int rollNo, String name, double marks) {
        this.rollNo = rollNo;
        this.name = name;
        this.marks = marks;
    }

    public double getMarks(){
        return marks;
    }

    public void setMarks(double marks){
        this.marks = marks;
    }

    public int getRollNo() {
        return rollNo;
    }

    public String getName(){
        return name;
    }
}

The code compiles and generates 2 different class files but I cannot run the program. I tried 2 ways from my terminal:

java com.StudentDemo . (Since package used is com).

Could you please point me in the right direction?

Newbie
  • 153
  • 3
  • 15

1 Answers1

1
  1. The source file should be in com/StudentDemo.java relative to the directory you're in.
  2. Compile with javac com/StudentDemo.java.
  3. Execute with java com.StudentDemo.

Most of this is clearly stated in the accepted answer to the duplicate.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I did put the StudentDemo.java in a folder "com" on the desktop. My pwd is desktop as well but when I compile using javac com/StudentDemo.java then I get javac: file not found: com/StudentDemo.java – Newbie Jan 22 '18 at 00:31
  • So you didn't put it there, or you weren't in that directory. – user207421 Jan 22 '18 at 00:37
  • when you say I wasnt in that directory, do you mean my pwd needs to be 'desktop' or should it be 'desktop/com' ? The latter would defeat the purpose of making a folder 'com'. I was able to compile the file from desktop but not run it. Why is there this discrepency? I checked the files are all in places like you said. Executing gives the same error as before "ClassNotFoundException". – Newbie Jan 22 '18 at 00:43
  • Your *cwd* needs to be 'desktop', the folder containing `com`, as per (1) above. By 'that directory' I meant the one you claimed you were in. The compiler doesn't lie. Examine your assumptions. Try `ls -l com` for example. Are you using *exactly* the `javac` command line above? Or are there other options? – user207421 Jan 22 '18 at 00:46
  • This is so weird. I did everything you said and still managed to screw it up. I ll keep working but I think I have exhausted my brain. – Newbie Jan 22 '18 at 00:50
  • point to be noted : When I remove the line 'pacakge com;', then the program starts working. How does 'package com;' relate with folder 'com' ? – Newbie Jan 22 '18 at 00:51
  • Well, how do you think it relates? Do you think it's just a coincidence? It isn't. And if you removed the package statement you would also have had to adjust both commands, and you haven't stated how. Please provide the result of `ls -l com`. – user207421 Jan 22 '18 at 00:58
  • I understand that during compilation the program is supposed to generate a 'com' folder but it isn't doing so (i guess). The result is :Desktop beeeeaasssst$ ls -l com total 24 -rw-r--r-- 1 beeeeaasssst staff 623 21 Jan 20:01 Student.class -rw-r--r-- 1 beeeeaasssst staff 1349 21 Jan 20:01 StudentDemo.class -rw-r--r--@ 1 beeeeaasssst staff 966 21 Jan 19:18 StudentDemo.java – Newbie Jan 22 '18 at 01:02
  • Eh? If a`com` directory isn't being generated what is the meaning of all your prior claims that it exists? And how do you explain the `ls -l com` output? Of course it is being generated. You aren't making sense. – user207421 Jan 22 '18 at 01:05
  • I had to make it manually and put the compiled filed in it later. If I run the program from desktop it doesnt generate 'desktop/com', and futhermore, doesnt put the compiled files in there. I hope that clarifies what I meant when I say 'com' isnt getting generated. – Newbie Jan 22 '18 at 01:12
  • Running the program doesn't generate directories. Compiling it might, if they don't already exist. According to your last comment under the deleted answer you don't have `com/StudentDemo.java` at all. You need to carry out steps 1-4 above exactly as specified and it will work. All the evidence is that you haven't done so, and all you're doing here is making contradictory statements. – user207421 Jan 22 '18 at 01:14
  • I meant compiling, not running.** pwd for all the steps is desktop**. Step 1) I have a folder 'com' (that I made) and I put StudentDemo.java inside it. Step 2) I compiled it and that works. The 2 generated classes are now inside com along with original file (StudentDemo.java). Step 3) Throws error: Error: Could not find or load main class com.StudentDemo Caused by: java.lang.ClassNotFoundException: com.StudentDemo – Newbie Jan 22 '18 at 01:33
  • I have more info. I wrote a hello world program on my desktop in the most simplest form. But it still gives me the same exception. Is something wrong with my computer? – Newbie Jan 22 '18 at 02:43