Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:30:27+00:00 2026-05-15T02:30:27+00:00

My goal is to find the package (as string) of a Java source file,

  • 0

My goal is to find the package (as string) of a Java source file, given as plaintext and not already sorted in folders.

I can’t just locate the first instance of the keyword package in the file, because it may appear inside a comment. So I was thinking about two alternatives:

  • Scan the file word-by-word, maintaining an “inside-a-comment” flag for the scanner. The first time the package keyword is encountered while not inside a comment, stop the scanning and report the result.
  • Use a regex – should be theoretically possible because block comments do not next in Java, but I tried making such a regex and it turned out to be quite complicated – for me, at least.

Another difference between the two approaches is that when scanning manually I can stop the scan when I can be certain the package keyword can no longer appear, saving some time… and I’m not sure I can do something similar with regexes. On the other hand, the decision “when it can no longer appear” is not necessarily simple, though I could use some heuristic for that.

I would like to hear any input on this problem, and would welcome any help with the regex. My solution is written in Java as well.

EDIT: to those suggesting actually parsing the file – it’s definitely a viable option, thank you, but it feels a bit of an overkill for me to parse the whole file for just the package. I’ll do it if there’s no simpler alternative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:30:27+00:00Added an answer on May 15, 2026 at 2:30 am

    I solved this problem by using a java parser. For my purpose javaparser was the best fit.

    CompilationUnit cu = JavaParser.parse( file );
    String packageName = cu.getPackage().getName().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal : How we can find the position of subView in a View. I
Goal: to find count of all words in a file. file contains 1000+ words
GOAL My goal is to find a text file or library that enables me
This is for the Apple platform. My end goal is to do a find
Goal : I wants when I drag image it become fade so we can
I'm maintaining a legacy Java servlet webapp ( nwp ). My goal is to
I am trying to do the following: <execution> <id>copy-jre</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration>
My goal: Find a DIV, slideUp all of it's child divs, then after all
Here is a JavaScript example from Eloquent JavaScript : function findSequence(goal) { function find(start,
My goal is to find out the length of the second field and if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.