| Author |
Doubt in static import
|
Srividhya Kiran
Ranch Hand
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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.)
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Srividhya Kiran
Ranch Hand
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Nov 24, 2005
Posts: 14670
|
|
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 ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
|
No I have only one copy of this file. May be there is something wrong with my compiler.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Try creating a brand new file with a new name: Does this one work?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
And what exactly do you get when you type... javac -version
|
 |
Srividhya Kiran
Ranch Hand
Joined: Feb 17, 2008
Posts: 166
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16686
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16686
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16686
|
|
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
Joined: Nov 24, 2005
Posts: 14670
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
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
Joined: Nov 24, 2005
Posts: 14670
|
|
|
You cannot do it that way. You need to set the PATH environment variable. Check this page.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
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
Joined: Sep 06, 2007
Posts: 142
|
|
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
Joined: Feb 17, 2008
Posts: 166
|
|
|
Thanks all I got it
|
 |
 |
|
|
subject: Doubt in static import
|
|
|