Is there an easier way to get the project name rather than parsing the path to the execution directory? JavaSE-1.6
Asked
Active
Viewed 7,834 times
2
-
6What's a "project name" to Java? That's usually managed by an IDE, and has nothing to do with Java itself. – mellamokb Oct 10 '11 at 23:30
-
how do you define `project name`? – d-live Oct 10 '11 at 23:31
-
Are you looking for System.getProperty("user.dir")? – Jeff Storey Oct 10 '11 at 23:31
-
Java doesn't have anything like a "project". Are you referring to IDE projects (e.g. projects in Eclipse, NetBeans or whatever you're using)? – Oct 10 '11 at 23:32
-
in non-static context, you could call `this.getClass().getPackage();` to get the package name. Maybe that'll help. – Bala R Oct 10 '11 at 23:32
-
For compiled Java, there is no notion of a project. There are classes and packages. – John B Oct 10 '11 at 23:32
-
Ah OK. I'm working with Eclipse. There it's called *project name* and it's the string of the folder which contains the whole application. – Lenar Hoyt Oct 10 '11 at 23:32
-
Create instance variable String proName; and implement setters and getters...[sarcasm] – TeaCupApp Oct 10 '11 at 23:34
2 Answers
3
Ah OK. I'm working with Eclipse. There it's called project name and it's the string of the folder which contains the whole application.
Firstly, this is an IDE-specific concept. Java (in general) has no concept of a project.
Second, it is probably not a good idea to make a program depend on the name of the Eclipse project. That will cause problems if you ever try to run your program independently of Eclipse and the build environment.

Stephen C
- 698,415
- 94
- 811
- 1,216
2
If you are making use of a jar archive file for your application and ant builds you can do the following;
- You can have the ant build file set a value (Project-name) in the MANIFEST.MF file, excample;
<manifest file="${basedir}\resources\jar\META-INF\MANIFEST.MF">
<attribute name="Manifest-Version" value="1.0" />
<attribute name="Version" value="${release.version}" />
<attribute name="Company" value="S1" />
<attribute name="Project" value="<project_name>" />
<attribute name="Java-Version" value="1.5" />
</manifest>
</target>
<target name="dist_jar" depends="create_manifest">
<delete file="${basedir}\build\jar\${jar.name}" />
<!--Create the JAR for the build-->
<jar jarfile="${basedir}\resources\jar\${jar.name}"
manifest="${basedir}\resources\jar\META-INF\MANIFEST.MF"
basedir="${jar.classes}" />
</target>
- Read the Project name from the MANIFEST.MF. See Read Manifest from Java code.
-
Hmmm ... is that the Eclipse project name, or the project name from the Ant build.xml file? – Stephen C Oct 11 '11 at 13:38
-
I just made it project name as an example. The closest thing I got to retrieving the project name from the .project eclipse file is: http://blog.vizio360.co.uk/2009/11/eclipse-get-project-name-within-ant-script/ – Koekiebox Oct 11 '11 at 13:54
-
Yo can also strip the ${basedir} property. See http://stackoverflow.com/questions/2454128/ant-basedir-and-eclipse-project-file-generation – Koekiebox Oct 11 '11 at 13:56