Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Enable assertions in current package.

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can some one help me to find out the reason why the following code does not throw AssertionError?
Test.java file is into 'Examples' directory and Test.java file is as -

Now Test.java is successfully compiled to produce 'one.Test.class'. Examples directory structure is as -

Current working directory on command line is Examples. Using java1.6. and Fedora os.
Now need to run Test class as -

I was expecting it to throw assertion error, because -ea:... enables assertions in the unnamed package in the current working directory. one is package inside Examples but somehow it looks like assertion remains disabled. It works very well if is used instead of .
What am i doing doing wrong? Is the symbol '...' is not correct to enable or disable assertions in current working directory.?

 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sharmila,

This code runs time with -ea option, as output is as mentioned below-



not sure what does :... option represent.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation says:

-enableassertions[:<package name>"..." | :<class name> ]
-ea[:<package name>"..." | :<class name> ]

Enable assertions. Assertions are disabled by default.

With no arguments, enableassertions or -ea enables assertions. With one argument ending in "...", the switch enables assertions in the specified package and any subpackages. If the argument is simply "...", the switch enables assertions in the unnamed package in the current working directory. With one argument not ending in "...", the switch enables assertions in the specified class.

If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. So, for example, to run a program with assertions enabled only in package com.wombat.fruitbat (and any subpackages), the following command could be used:

java -ea:com.wombat.fruitbat... <Main Class>

The -enableassertions and -ea switches apply to all s loaders and to system classes (which do not have a class loader). There is one exception to this rule: in their no-argument form, the switches do not apply to system. This makes it easy to turn on asserts in all classes except for system classes. A separate switch is provided to enable asserts in all system classes; see -enablesystemassertions below.


So when you use just "-ea:...", it enables assertions in the unnamed package in the current working directory. It does not enable assertions in your package 'one'.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jesper,
I tried the code and it ran as explained but I could not understand the meaning of unnamed package. Could you please explain the meaning of unnamed package in this context
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper and salil,
Thanks,
The problem was what is mean by unnamed package?
Let me show the things how it worked.

And Test.java is in unnamed package. See the source of Test.java

It does not have package statement. So unnamed package mean no package statement in souce file.
So on command line if your current directory is Examples as -

then assertion is enable only in 'Test.class' and 'Another.class'. And assertion is not enable in package 'one'.
Regards
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just put Java source and class files in a directory, without any 'package' statement in the file. Those classes will then be in the unnamed package.

Suppose I have a class Test, and I don't put any 'package' statement in the file. I put the directory that contains Test.class in the classpath, and run it like this:

java Test

(note, no package name).

Sharmila explained it correctly.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for explaining me the sense of unnamed package
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic