0

I am trying to learn how to compile classes in Linux using javac and running them using java commands.

If I create a class and its main method is simply System.out.println(1); then I have no issues compiling.

However, when I start to reference other classes in the main method of a class, such as

package com.example

public class Main { 

  public static void main(String[] args) {

    Class1 class = new Class1();
    class.printX();

  } 
}

It will not let me compile even though Class1 is already compiled. I literally just type in javac Main.java in the terminal and it replies with a few errors about how iv written the class eg. Class1 class = new Class1(); is not a statement ect.

Through some simple google searches, I have come across terms such as CLASSPATH that I don't fully understand. I have literally just downloaded the JDK and tried to compile away lol.

I am completely new to Java without using an IDE and I've only just started to use the Linux OS as of yesterday (Linux Mint) so do assume I know absolutely nothing. I am also new to posting on stackoverflow so please go easy on me if I'v done something wrong, I'v tried to supply enough information without rambling on. Thanks!

4
  • 1
    Some formatting please? Commented Nov 1, 2016 at 1:29
  • to compile packages you should be at the top-level of the package in the terminal, in your example in the terminal typing ls should list the com folder of your package, then compile javac -cp . com/example/Main.java. The -cp (or -classpath) flag to javac tells it option to specify the base directory of the package com.example, in order to locate Main.java Commented Nov 1, 2016 at 1:43
  • I still get the same errors eg. "error: not a statement". I'm thinking when I compile it, it just does not know about Class1, despite the fact that Class1.class and Class1.java are in the same folder as Main.java. Not sure why, if that is indeed the case.. Commented Nov 1, 2016 at 1:52
  • Do I have to have my directories set up in a certain way, so that it can find all classes within a package? Everything in my Main class would seem foreign to the system trying to compile it if it doesnt know where to grab Class1.java from.. Commented Nov 1, 2016 at 4:22

1 Answer 1

1

I believe you should not use 'class' as a variable name. It's a key word in Java.

Sign up to request clarification or add additional context in comments.

2 Comments

If you are ever interested in using an IDE like Eclipse or Intellij, they do auto compiling for you, and will raise red line error in this case under 'class' suggesting 'delete this token'.
Thank you so much. I am an absolute idiot. I swear I was doing everything correctly, yet I was getting errors! I have been spending ALL day on this, just to be defeated by something so simple XD I normally use netbeans, but I wanted to add compiling from the terminal to my skill set, even if Id never have to use it in the future. Again, thanks so much!

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.