Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in static import

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks

when I try to run this program from K & B pg 776.



I am getting identifier expected error in line 1 and line 2.

Thanks
Srividhya
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static imports were introduced with version 1.5. My guess is that you're running a previous version.

At the command prompt, when you type...

javac -version

What do you see? (You might get a lengthy error message because you didn't specify a source file, but at the top you should see the version.)
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc

I am using jdk1.6. It just displayed me the error message I said in my previous post saying "Identifier Expected".

Srividhya
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, next possibility...

In a source file, the only thing that can come before import statements is a package declaration (or a comment). Do you have anything above the import statements?
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Marc I have given you the exact source file in previous post and there is no statement before import.
[ April 09, 2008: Message edited by: Srividhya Kiran ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have given you the exact source file

So you mean that you have copy/pasted TestStaticImport.java here, right ? How did you compile it ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srividhya Kiran:
No Marc I have given you the exact source file in previous post and there is no statement before import...

Hmmm, I copied your post exactly and it compiles and runs (in version 1.6) with the following output...

Are you certain you are using the correct source file in the correct directory? Is it possible that you have different copies of this file?
[ April 09, 2008: Message edited by: marc weber ]
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I have only one copy of this file. May be there is something wrong with my compiler.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try creating a brand new file with a new name:

Does this one work?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be there is something wrong with my compiler.

You still didn't tell us how you compiled this source. And also, can you copy/paste the exact error message ?
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chirstophe

My Exact Source file TestStaticImport.java is below:



C:\Java\jdk1.6.0_04\bin>javac TestStaticImport.java
TestStaticImport.java:1: <identifier> expected
import static java.lang.System.out; // 1
TestStaticImport.java:1: '.' expected
import static java.lang.System.out; // 1
TestStaticImport.java:2: <identifier> expected
import static java.lang.Integer.*; // 2
TestStaticImport.java:2: '.' expected
import static java.lang.Integer.*; // 2
4 errors

This is Error message I am getting.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what exactly do you get when you type...

javac -version
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc when I gave



So I gave
C:\Java\jdk1.6.0_04\bin\DirA>java -version
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

which displayed me the above message.

Srividhya
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Java\jdk1.6.0_04\bin>javac TestStaticImport.java

First of all, it is *not* a good idea to put Java source code in the bin directory.

Second, just because you are in the bin directory doesn't mean that you are running the correct version of Java. It still depends on the path.

Run the following and tell us the output:

java -version
javac -version

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


An "invalid flag" means that the javac version is 1.4 or earlier.

To confirn the exact version use:

javac -J-version

Henry
[ April 09, 2008: Message edited by: Henry Wong ]
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc

When I tried to compile the code you gave it worked.



I cant understand what is the problem with my previous program

Srividhya
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry

This is the message I get

C:\Java\jdk1.6.0_04\bin>javac -J-version
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run javac version again... you can't get an invalid option and the message that you got. Basically, run both of these commands again and give us the output...

javac -J-version
javac -version


EDIT: Now that I think about it, it is possible to have an old java compiler executed by a newer jvm -- which doesn't really make sense. By any chance, did you happen to install the JRE instead of the JDK? In there a "javac" program actually in the bin directory that you are in?

Henry
[ April 09, 2008: Message edited by: Henry Wong ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac: invalid flag: -version

Here we are. You are using an old compiler. Your PATH is probably not set properly. Make sure that your JDK6 bin directory is in the path, and that other older jdks are not declared before it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srividhya Kiran:
...This is the message I get

C:\Java\jdk1.6.0_04\bin>javac -J-version
java version "1.6.0_04"...

Interstingly, your current directory is "bin" here. When you got the response that -version is an invalid flag, your current directory was C:\Java\jdk1.6.0_04\bin\DirA. (I'm guessing that when you successfully ran my class PrintingOut, this was also under the bin directory.)

As Henry pointed out, you do not want to mix your own files and directories in with Java's bin content. As he also pointed out (and as Christophe agrees), this sounds like a path issue. To correct this, see step 4 of Sun's installation instructions for Windows. Note the following from these instructions...
  • PATH environment variable is a series of directories separated by semi-colons (;) and is not case sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
  • You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored. If one is already present, update it to jdk1.6.0_<version>\bin.
  • I am fairly confident that your PATH variable includes an old JDK version (1.4 or earlier), and you should update this to point to your 1.6 bin directory.
    [ April 09, 2008: Message edited by: marc weber ]
     
    Srividhya Kiran
    Ranch Hand
    Posts: 166
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes chirtophe you are correct. I understood what the problem is. In the bin directory compiler gave me the correct version 1.6. But under DirA where my program is, the version displayed is below:

    C:\Java\jdk1.6.0_04\bin>javac -J-version
    java version "1.6.0_04"
    Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
    Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

    C:\Java\jdk1.6.0_04\bin\DirA>javac -J-version
    java version "1.4.2_16"

    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_16-b05)
    Java HotSpot(TM) Client VM (build 1.4.2_16-b05, mixed mode)

    Now how should I change the version and compile.So I gave

    C:\Java\jdk1.6.0_04\bin\DirA>javac -source 1.6.0_04 TestStaticImport.java
    javac: invalid source release: 1.6.0_04

    which gave me the above error.

    Srividhya
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You cannot do it that way. You need to set the PATH environment variable. Check this page.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Srividhya Kiran:
    ...how should I change the version and compile...

    See the link I provided above to Sun's installation instructions. Follow step 4 carefully.
     
    Nancy Antony
    Ranch Hand
    Posts: 145
    Java Linux Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To set the PATH variable you can do the following

    invoke control panel -> go to system -> advanced -> select environment variables

    Then in user variables and system variables check for variable PATH and the folderofjdk\bin be the first one or if previous version bin is in path replace it with the new one, do the same with the classpath variable too, prepend folderofjdk\lib\tools.jar;.; in the classpath.

    create a separate folder outside jdk and store all your files there. As jdk\bin is in path, you'll be able to invoke javac and java commands from anywhere in your system.

    Nancy
     
    Srividhya Kiran
    Ranch Hand
    Posts: 166
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all I got it
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic