0

I recently began learning Java with an online tutorial series. The website provides an online Java editor and interpreter, as they do not want viewers needing to install any programs. However, I would like to follow along in Sublime Text 3.

I've created a small test file (.java) of basic statements using correct syntax, except I do not have a package for it set up. When I run the file (in Sublime's Java interpreter), of course an error occurs where the package statement should be.

The tutorial series does not speak about packages whatsoever. I've looked at numerous resources, but they tell me that a package is just a directory for related classes and how to create a package for a file using the Windows Command Prompt with javac, which I do not have.

I know a package is a folder and a directory for related classes/ class files, but:

  • How do I set up a package for my class file(s)?
  • Is a package even necessary if I only have one class file? I would assume so as the error is occurring where the package statement would be.

EDIT: Code:

package test;

public class TestFile

{

    public static void main(String [] args)

    {

    //Declaring variables
    byte a = 64;
    short b = -385;
    int c = 100000;
    long d = 2000000;

    float e = 16.5;
    double f = 121.654;

    boolean g = true;

    char h = 'H';
    String i = "Some string.";

    String [] j = {"Some string.", "Another string.", "Yet another string."};

    //Printing
    System.out.println(i);
    System.out.println("String i = " + i);

    //If/ else if/ else construction
    if (i.length() > 4)

    {
    System.out.println("String is longer than four characters.");
    }

    else if (i.length() == 4)

    {
    System.out.println("String is exactly four characters long.");
    }

    else

    {
    System.out.println("String is shorter than four characters.");
    }


    }
}

Error:

  File "C:\Users\Owner\Desktop\Code\Java\Test.java", line 1
    package test;
               ^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
Jacob
  • 268
  • 1
  • 7
  • 20
  • Well, I can build that file in Sublime 2 (without the package statement). What was your first error. – Tom Feb 18 '15 at 16:25

2 Answers2

1

Place TestFile.java under test(create a folder C:\Users\Owner\Desktop\Code\Java\test)

Note:It is not required to use package name it only uses for code seperation i.e. having similar code in one package

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • Place `Test.java` in `test` so that it contains both `Test.java` and `TestFile.class`? I now get an error, `python: can't open file 'C:\Users\Owner\Desktop\Code\Java\Test.java': [Errno 2] No such file or directory [Finished in 0.1s]`. I don't know why it says Python for building. – Jacob Feb 18 '15 at 16:26
  • @Jacob you can remove package test; its not nessasry.If you want to use package then first create a test folder then place `TestFile.java` inside `test` then compile – singhakash Feb 18 '15 at 16:52
1

Package is more than just a folder. It is a fundamental aspect of Java. However as you mention, if you have only a single class it is pretty much irrelevant, and in that case it is ok to remove the package declaration from your class (to use the 'default' package, this is called).

If you leave package declared, then you have to tell Javac where to look for the file, relative to the base (default) package.

See here for example

Put this in c:\code (name it Test.java)

public class Test {
    public static void main(String args[]) {
        System.out.println("hello");
    }
}

do this:

c:
cd\
cd code
javac Test.java
java Test
hello
Richard
  • 1,070
  • 9
  • 22
  • So how do I use the 'default package'? Do I simply omit the package statement altogether? What do I do with my files/ How do I organize them? – Jacob Feb 18 '15 at 16:33
  • I've added more detail to my answer – Richard Feb 18 '15 at 17:03
  • I do not have Javac. (Although this is straying from the question's topic,) where can I download Javac and install it? – Jacob Feb 18 '15 at 17:20
  • Just download JDK 7 : http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html It sounds to me as if this interpreter you're using will not understand the package declaration anyway. Just remove the package declaration. – Richard Feb 18 '15 at 17:24
  • If you're serious about learning Java... after installing the JDK you should probably download and install eclipse too : https://eclipse.org/downloads/packages/eclipse-ide-java-developers/lunasr1a eclipse is an 'integrated development environment' It makes writing Java MUCH MUCH easier, includes things like syntax checking etc. – Richard Feb 18 '15 at 17:27
  • I've installed JDK 7 into my program files. Using the command prompt, I changed directory to my Code folder and then entered `javac Test.java`. The prompt still says 'javac' is not recognized. Does this have to do with my PATH variable now? (I will definitely consider and IDE when I learn more.) – Jacob Feb 18 '15 at 17:33
  • Yes Jacob, it's a path problem. Just put program files... jdk1..../bin... in your path – Richard Feb 18 '15 at 17:43
  • @Jacob set environment variable for java https://www.java.com/en/download/help/path.xml – singhakash Feb 18 '15 at 17:45
  • Currently my PATH variable contains Python34 at the end: ...C:\Program Files\Dell\DW WLAN Card;C:\Python34. I can't append jdk/bin to it, can I? – Jacob Feb 18 '15 at 17:48
  • yeah sure.. put a ";" then the path.. I have to go home how.. you can find out how to do this just by googling. – Richard Feb 18 '15 at 17:50
  • @Jacob you can just put a semicolon and paste a path upto bin directory of java – singhakash Feb 18 '15 at 17:50
  • @singhakash How so? I don't understand what you're saying. – Jacob Feb 18 '15 at 17:51
  • I know how to access System Variables via Control Panel/Advanced System Settings, but what should my PATH variable look like exactly? Currently it is: `...C:\Program Files\Dell\DW WLAN Card;C:\Python34`. Should it look like: `...C:\Program Files\Dell\DW WLAN Card;C:\Python34;jdk1/bin`? Python34 is just on the C drive, but Java is in C:\Program Files\. Sorry, I'm just a bit confused. – Jacob Feb 18 '15 at 18:03
  • @Jacob you have to paste the full path of java `C:\foder1\forder2\bin` after semicolon – singhakash Feb 18 '15 at 18:25
  • @singhakash Okay, I have done that now with the PATH variable and it's successfully configured. The `javac` command is still not recognized. – Jacob Feb 18 '15 at 18:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71195/discussion-between-singhakash-and-jacob). – singhakash Feb 18 '15 at 18:35