SUMMARY ORDER
Appellant Salvador Diaz, pro se, appeals from the district court’s sua sponte dismissal of his complaint on claim preclusion grounds. We assume the parties’ familiarity with the underlying facts, procedural history of the case, and specification of the issues on appeal.
This Court reviews de novo a district court’s sua sponte dismissal of a complaint, see Giano v. Goord, 250 F.3d 146, 149-50 (2d Cir.2001), and the application of the doctrine of res judicata, see O’Connor v. Pierson, 568 F.3d 64, 69 (2d Cir. 2009). “The preclusive effect of a federal-court judgment is determined by federal common law,” and “is defined by claim preclusion and issue preclusion, which are collectively referred to as ‘res judicata.’ ” Taylor v. Sturgell, 553 U.S. 880, 891-92, 128 S.Ct. 2161, 171 L.Ed.2d 155 (2008). Claim preclusion “forecloses successive litigation of the very same claim, whether or not relitigation of the claim raises the same issues as the earlier suit.” Id. at 892, 128 S.Ct. 2161 (quoting New Hampshire v. Maine, 532 U.S. 742, 748, 121 S.Ct. 1808, 149 L.Ed.2d 968 (2001)) (internal quotation marks omitted). “Issue preclusion, in contrast, bars ‘successive litigation of an issue of fact or law actually litigated and resolved in a valid court determination essential to the prior judgment,’ even if the issue recurs in the context of a different claim.” Id. (quoting New Hampshire, 532 U.S. at 748-49, 121 S.Ct. 1808).
Under federal law, claim preclusion “bars later litigation if an earlier decision was (1) a final judgment on the merits, (2) by a court of competent jurisdiction, (3) in a case involving the same parties or then-privies, and (4) involving the same cause of action.” EDP Med. Computer Sys., Inc. v. United States, 480 F.3d 621, 624 (2d Cir. 2007) (alteration and internal quotation marks omitted). Although claim preclusion is an affirmative defense that must ordinarily be raised in a party’s answer or else is waived, a district court may in certain circumstances raise the issue sua sponte. See Salahuddin v. Jones, 992 F.2d 447, 449 (2d Cir.1993) (per curiam).
Diaz’s instant complaint specifically sought, inter alia, a “declaratory judgment voiding his conviction by court-martial,” a claim over which the district court had determined that it lacked subject matter jurisdiction in Diaz’s prior action. A dismissal without prejudice for lack of subject matter jurisdiction is not a dismissal “on the merits” for claim preclusion purposes. See Fed.R.Civ.P. 41(b) (“Unless the dis*344missal order states otherwise, a dismissal order under [the involuntary dismissal provision] and any dismissal not under this rule — except one for lack of jurisdiction, improper venue, or failure to join a party under Rule 19 — operates as an adjudication on the merits.” (emphasis added)); Hernandez v. Conriv Realty Assocs., 182 F.3d 121, 123 (2d Cir.1999) (“[W]here a court lacks subject matter jurisdiction, it also lacks the power to dismiss with prejudice.”). While Diaz’s instant claim seeking declaratory relief was previously dismissed without prejudice, in 2008 the district court dismissed his ineffective assistance of counsel and erroneous review allegations with prejudice, as those claims had already been fully litigated in the District of Kansas and the Tenth Circuit. See Diaz v. Dep’t of Def., No. 08 Civ. 370(DC), 2008 WL 4671833, at *4 (S.D.N.Y. Oct.23, 2008). Those claims, Diaz concedes, “are essential to demonstrate the claim in the instant case.” Appellant’s Br. 14-15. Accordingly, because those claims were previously dismissed on the merits (i.e., on the ground of issue preclusion), the district court was correct in concluding that these claims were barred as res judicata and therefore in dismissing Diaz’s instant complaint for a declaratory judgment. See, e.g., Bank of N.Y. v. First Millennium, Inc., 607 F.3d 905, 918 (2d Cir.2010).
We have considered Diaz’s remaining arguments and find them to be without merit. For the foregoing reasons, the judgment of the district court is hereby AFFIRMED.