• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

assert - Usage

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to use the new assert keyword, and when i try compiling it with javac -source 1.4 <file>.java, I get a error saying source is an invalid switch. The version of jdk is 1.4.0_03 . Is there any environment setup to be done , or is the jdk version not correct ?
Thanks,
Uma.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Uma!
Have you tried executing java -? to see the options allowed?
[ 9:52:03,63] C:\>java -?
Usage: java [-options] class [args...]
(to execute a class)
or java -jar [-options] jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp -classpath <directories and zip/jar files separated by ;>
set search path for application classes and resources
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-showversion print product version and continue
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
 
Uma Balu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The sample code i used was
class AssertDemo {
static int val = 3 ;

static int getNum() {
return val-- ;
}
public static void main(String[] aa) {
int i = 0 ;
for(int j = 0; j<10; j++)
{
i = getNum() ;
assert n>0 ;
System.out.println("n is = " + n) ;
}
}
}
Am not able to compile the code at all, the javac compiler is not able to recognise the keyword assert, i got this error
C:\uma\java>javac AssertDemo.java
AssertDemo.java:17: ';' expected
assert n>0 ;
^
AssertDemo.java:17: cannot resolve symbol
symbol : class assert
location: class AssertDemo
assert n>0 ;
^
2 errors
hence i tried reading the usage from the sun site and tried using "javac -source 1.4" but, the option source was not recognized and is not listed when i tried using...
C:\uma\java>javac -source 1.4 AssertDemo.java
javac: invalid flag: -source
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-O Optimize; may hinder debugging or enlarge class file
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-target <release> Generate class files for specific VM version
java version "1.4.0_03" was used...
How do i resolve this?
Thanks,
Uma..
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uma-
Let's put the code using the "code" tags, it looks cleaner:

Notice the comments. You were asserting with a variable n, but you hadn't even declared.
Now, I'm using j2sdk1.4.1_02:
C:\j2sdk1.4.1_02\bin>javac -source 1.4 Test.java
compiles with no problem. Now:
C:\j2sdk1.4.1_02\bin>java -ea Test
n is = 3
n is = 2
n is = 1
Exception in thread "main" java.lang.AssertionError
at Test.main(Test.java:16)
assertions are disable by default, so you have to use -ea if you want to enable assertions.
hope this helps
 
Armando Anton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure about the java version you are using?
Try "java -version" in the command line
[ 9:52:17,48] C:\>java -version
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
 
Uma Balu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
I tried installing the jre for 1.4.1, now the java -version gives me "1.4.1_02", but again -source is not been recognised
Uma...
 
Armando Anton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my javac -? (i wrote before my java -? when i wanted to write this)
[ 9:52:51,44] C:\>javac -?
javac: invalid flag: -?
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-O Optimize; may hinder debugging or enlarge class file
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-help Print a synopsis of standard options
I think you have some problems with your PATH enviroment variable (perhaps you have got different javac.exe files in different paths)
To be sure you are using the addecuate version go to the directory where you have installed JAVA, then go to the subdirectory bin and type 'javac.exe -?' to see the options allowed and if the source option is allowed then when compiling use [PATH_TO_JAVA]/bin/javac.exe -source 1.4 Source (or fix the problem with the PATH variable)
Hope it helps
[ August 22, 2003: Message edited by: Armando Anton ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Uma
Check and make sure that the classpath is not set to an older version. You will need to change the classpath when you change a version (if installatiuon path is different).
 
Uma Balu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks for the replies... the problem was with my JDK version, I had jdk1.4.0_03 and jre version jre1.4.1, the "javac" compiler of 1.4.0, does not support -source flag...
Thanks ,
Uma..
 
reply
    Bookmark Topic Watch Topic
  • New Topic