Since contradictions in Ecclesiastes were mentioned, the Gemara cites additional relevant sources. Rav Yehuda, son of Rav Shmuel bar Sheilat, said in the name of Rav: The Sages sought to suppress the book of Ecclesiastes and declare it apocryphal because its statements contradict each other and it is liable to confuse its readers. And why did they not suppress it? Because its beginning consists of matters of Torah and its end consists of matters of Torah. The ostensibly contradictory details are secondary to the essence of the book, which is Torah. The Gemara elaborates: Its beginning consists of matters of Torah, as it is written: “What profit has man of all his labor which he labors under the sun?” (Ecclesiastes 1:3), and the Sages of the school of Rabbi Yannai said: By inference: Under the sun is where man has no profit from his labor; however, before the sun, i.e., when engaged in the study of Torah, which preceded the sun, he does have profit. Its ending consists of matters of Torah, as it is written: “The end of the matter, all having been heard: Fear God, and keep His mitzvot; for this is the whole man” (Ecclesiastes 12:13). With regard to this verse, the Gemara asks: What is the meaning of the phrase: For this is the whole man? Rabbi Eliezer said: The entire world was only created for this person. Rabbi Abba bar Kahana said: This person is equivalent to the entire world. Shimon ben Azzai says and some say that Shimon ben Zoma says: The entire world was only created as companion to this man, so that he will not be alone. And to the essence of the matter, the Gemara asks: What is the meaning of: Its statements that contradict each other? It is written: “Vexation is better than laughter” (Ecclesiastes 7:3), and it is written: “I said of laughter: It is praiseworthy” (Ecclesiastes 2:2), which is understood to mean that laughter is commendable. Likewise in one verse it is written: “So I commended mirth” (Ecclesiastes 8:15), and in another verse it is written: “And of mirth: What does it accomplish?” (Ecclesiastes 2:2). The Gemara answers: This is not difficult, as the contradiction can be resolved. Vexation is better than laughter means: The vexation of the Holy One, Blessed be He, toward the righteous in this world is preferable to the laughter which the Holy One, Blessed be He, laughs with the wicked in this world by showering them with goodness. I said of laughter: It is praiseworthy, that is the laughter which the Holy One, Blessed be He, laughs with the righteous in the World-to-Come. Similarly, “So I commended mirth,” that is the joy of a mitzva. “And of mirth: What does it accomplish?” that is joy that is not the joy of a mitzva. The praise of joy mentioned here is to teach you that the Divine Presence rests upon an individual neither from an atmosphere of sadness, nor from an atmosphere of laziness, nor from an atmosphere of laughter, nor from an atmosphere of frivolity, nor from an atmosphere of idle conversation, nor from an atmosphere of idle chatter, but rather from an atmosphere imbued with the joy of a mitzva. As it was stated with regard to Elisha that after he became angry at the king of Israel, his prophetic spirit left him until he requested: “But now bring me a minstrel; and it came to pass, when the minstrel played, that the hand of the Lord came upon him” (II Kings 3:15). Rav Yehuda said: And, so too, one should be joyful before stating a matter of halakha. Rava said: And, so too, one should be joyful before going to sleep in order to have a good dream. The Gemara asks: Is that so, that one should introduce matters of halakha joyfully? Didn’t Rav Giddel say that Rav said: Any Torah scholar who sits before his teacher and his lips are not dripping with myrrh due to fear of his teacher, those lips shall be burnt, as it is stated: “His lips are as lilies, dripping with flowing myrrh [shoshanim notefot mor over]” (Song of Songs 5:13)? He interpreted homiletically: Do not read mor over, flowing myrrh; rather, read mar over, flowing bitterness. Likewise, do not read shoshanim, lilies; rather, read sheshonim, that are studying, meaning that lips that are studying Torah must be full of bitterness. The Gemara explains: This is not difficult, there is no contradiction here, as this, where it was taught that one should introduce matters of halakha joyfully, is referring to a rabbi, and that, where it was taught that one must be filled with bitterness, is referring to a student, who must listen to his teacher with trepidation. And if you wish, say instead that this and that are referring to a rabbi, and it is not difficult. This, where it was taught that he must be joyful, is before he begins teaching, whereas that, where it was taught that he must be filled with bitterness and trepidation, is after he already began teaching halakha. That explanation is like that which Rabba did. Before he began teaching halakha to the Sages, he would say something humorous and the Sages would be cheered. Ultimately, he sat in trepidation and began teaching the halakha.
The truth is, technically speaking there is no obligation in the Torah that we be happy. There is a verse in Psalms that states we should serve God with joy (100:2). Likewise the Torah teaches us the terrible punishments which will befall Israel for rejecting God and His Torah, and in those curses it adds, “[All of this will be] on account that you did not serve Me with happiness and goodness of the heart…” (Deut. 28:47). However, strictly speaking, those verses do not instruct us to be happy at all times, just that we serve God with happiness.
There is additionally a well-known expression (and song): “It is a great mitzvah to be happy constantly.” But this too is not a verse or a statement from a classic text. It appears in the writings of Rabbi Nachman of Breslov (Tanina 24), and though important, must be viewed as sage counsel rather than a strict obligation.
Finally, there is a verse in the Torah that we should rejoice in all the good God has granted us (Deut. 26:11) – that we be happy about our blessings. However, this too is not instructing us to be happy at all times and for everything, only for the good. In addition, the commentators do not view this as a strict obligation, one of the 613 commandments, but rather as an ideal to strive for.
Why does the Torah not command us to be happy? No doubt it is for the reason you wrote. Happiness is not in our direct control. We can strive to be happy but we cannot simply turn it on if we do not feel it. The Torah gives us realistic mitzvot alone – ones in our control to fulfill. Thus, it almost always addresses the realm of action rather than the realm of thought. We can be commanded not to eat pork but it’s impossible to command us not to be sad.
Even so, it is clear from many statements of the Sages that we should strive for happiness and cheerfulness (e.g., Talmud Sanhedrin 100b, Brachot 31a, 60b, Shabbat 30b, Ketubot 62a, Pirkei Avot 6:6). Rabbi Akiva, as well as his teacher Nachum Ish Gamzu, were known for accepting all that God granted them happily.
The reason that happiness is so integral is clear. People who are positive and excited about life will serve God with energy. They will be able to grow and to face challenges, rather than sinking into listlessness and depression. An early Hassidic Rebbe was likewise rerported to have said, “Although there is no mitzvah to be happy, happiness can bring one to the greatest of mitzvot.”
How does one acquire happiness? Ultimately it should stem from our belief in God. The stronger we believe in our hearts that God loves us and only give what is best for us, the less we will be frustrated and annoyed with how our lives are going. We will see everything in life as just right for us, and as opportunities specially crafted for us by a loving God, in order that we grow and maximize our potential.
But again, we cannot just turn on happiness. It is a skill, which as all skills must be learned and developed. We have a lot of material on how a person can acquire happiness. See here for some important links: