Java get project name

How to get the project name in eclipse?

How do I get the name of the current eclipse project? I’m in a GMF view and need the projectname when some submenu of the popup menu ist used.

4 Answers 4

This GMF class has a straightforward answer, if you have access to the ResourcesPlugin name:

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(myBundleName); 

The generic answer (from a potentially outdated code) could be like (if you have an editor opened):

IEditorPart editorPart = getSite().getWorkbenchWindow().getActivePage().getActiveEditor(); if(editorPart != null) < IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ; IFile file = input.getFile(); IProject activeProject = file.getProject(); String activeProjectName = activeProject.getName(); //. use activeProjectName >
 IViewPart [] parts = MyPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews(); IProject activeProject = null; for(int i=0;i > String activeProjectName = activeProject .getName(); 

For GMF change the MyPlugin part to: IViewPart[] parts = DiagramEditorPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews(); Your GMF plugin name can be found in your .genmodel.

Using the selection service will give you the currently selected object, you can then check the selection type and get the project based on the selection.

If you create an ISelectionListener and register as a listener on the ISelectionService, you’ll be notified whenever the active selection changes, and be given a reference to the selection and the owning part.

ISelectionListener listener = new ISelectionListener() < public void selectionChanged(IWorkbenchPart sourcePart, ISelection selection) < setSourcePart(sourcePart); setSelection(selection); >>; . //register the listener selectionService.addSelectionListener(listener); . //either get the selection service and get the selection, or use the stored version from the listener ISelectionService selectionService = Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService(); ISelection selection = selectionService.getSelection(); if(selection instanceof IStructuredSelection) < Object element = ((IStructuredSelection)selection).getFirstElement(); IProject project; if (element instanceof IResource) < project= ((IResource)element).getProject(); >else if (element instanceof PackageFragmentRootContainer) < IJavaProject jProject = ((PackageFragmentRootContainer)element).getJavaProject(); project = jProject.getProject(); >else if (element instanceof IJavaElement) < IJavaProject jProject= ((IJavaElement)element).getJavaProject(); project = jProject.getProject(); >> else if (selection instanceof ITextSelection) < if(sourcePart instanceof JavaEditor) < IJavaElement element = SelectionConverter.resolveEnclosingElement(sourcePart, selection); project = element.getJavaProject().getProject(); >> 

Источник

How to get the name of a Java program?

I’m creating an application that could be either an .exe or a .jar and I need to know which it is. Does anyone know how I can get the file name/extension of a program running in Java please? I can get the path of the program running but I can’t get the name.

Читайте также:  Java хранимые процедуры jdbc

The question is unclear. Process A is launched via a launcher (.exe or .jar) do you want to find out the details of the launcher in process A or in some other process B. What is java based? process A or process B?

3 Answers 3

Make ‘name of program’ a property that is passed to your program via ‘-D’ command-line switch, like so

java -Dprogram.name=myApp.jar -jar myApp.jar 

Read it in your code like so

if ("myApp.jar".equals(System.getProperty("program.name"))) < // perform appropriate actions. >

The actual program running the JAR file would be java.exe .

I suggest you approach the problem from a completely different angle and have the exe wrapper set a system property that the program queries. Or you could have it and the JAR manifest specify different main classes.

Hi, now I see the implication — thank you for your reply. I will try what you have suggested but I will find a work around somehow!

This snippet below should do the trick:

The output will look like this:

$ java -jar CasaInformatizada-0.1.4.jar dez 03, 2020 5:27:49 PM br.com.cvra.monitorArduino.Principal subirEscrivao INFORMAÇÕES: /var/tmp/monitorArduino.xml nomeDaClasse = br/com/cvra/monitorArduino/Principal.class _nomeArquivoJava = /var/tmp/CasaInformatizada-0.1.4.jar 

This is based upon the follow sources:

Please explain what your code does and how it does it. Also, please explain where the links go to, how they relate to the question, and how they help.

The snippet retrieves the path of the jar file which is running it. It works using the classLoader of the class instantiated by String, then using the full name of the class, with periods replaced by foward slashes, and adding «.class» to makes the class file name that is used to get the absolute path of the file running the application. The links go to an article about jar files (the first) and to a open reference about classLoader (the second), handling different parts of the snippet suggested, and giving more details about each one of it’s original parts to complete the understanding.

Читайте также:  My first php code

Источник

Java: Get the name of my project from code

I’m trying to achieve what I thought would be a simple thing. Suppose I have the following folder structure:

myProjectName/src/com/company/Class1 myProjectName/src/com/otherCompany/somePackage/Class2 

I want to write a piece of code that if called from either Class1 or Class 2 will simply return myProjectName So basically I just want to get the name of my Java project (not the current working directory!) Couldn’t find anything online. I have looked at reflection, but with no luck.

you can get the directory where your .class file is in, but there is no way, at execution time, to find out where the source files were (as I know). The folder name were you put the source file is arbitrary, eventually just matters for the IDE, at compile time. To find the directory, I believe you can use the ClassLoader

I have added to the question that you did look at reflection. A show of effort is important here — people want to see that you tried. You could edit it further and show the actual code you tried when using reflection.

@GeorgeCimpoies also, maybe, to some degree, use the class path (see System.getProperties() ) — but not really reliable. I also remember using the ProtectionDomain to get the JAR file of my source. getClass().getProtectionDomain().getCodeSource() — ok, to some degree using reflection

3 Answers 3

Simple spoken: there is no good (built-in) way to achieve that.

myProjectName/src/com/company/Class1 

Only the package name com.company.Class1 is visible at runtime. There is absolutely no notion within the Java language that covers that myProjectName part.

In other words: that name is meta information. Yes, the answer by Carlos gives you a way to access that, but I still would consider that more of a hack than a solution.

A better approach: simply create a class like ProjectInfo , or maybe ComponentInfo that carries such information.

Meaning: don’t rely on file system names. If you have something that belongs to a project/component/unit/. then make that «concept» part of your source code. The intention of the ProtectionDomain and CodeSource classes is much more about security and such things. It is not meant as «container» for such meta information as a «project name».

Читайте также:  Copy element with css

Источник

java: find project name from within class

How can I use java reflection or similar to find project name by just having an instance of the class? If not the project name — which is what I really want — can I find the package name?

What do you mean by project name? The one you see in your IDE? If you want to know package name provided you have an instance of a class you can do it this way: instance.getClass().getPackage()

3 Answers 3

Projects are simply organizational tools used by IDEs, and therefore the project name is not information contained in the class or the JVM.

To get the package, use Class#getPackage(). Then, Package#getName() can be called to get the package as the String you see in your code’s package declaration.

Class clazz = Application.class; Package package = clazz.getPackage(); System.out.println(package.getName()); 

Project name is located in your IDE’s configure file, eg. it’s .project in Eclipse. It’s a xml file, project name is located in the projectDescription element’s child element name.

Project Name is not known to Java, but is typically the name of the directory where your code is.

For example, if you are working on a project (in Eclipse or Intellij) and that project is in the folder «~/eclipse-workspace/myproject» then the project name you want is probably «myproject». Be aware that this will not be universal — or even applicable outside of your development environment.

So what you probably want is the user dir, which in those cases will coincide with the project name. If someone just has a binary (jar or class file), it will be where it is executed from — which may not be the same.

But, if that’s what you want, use the following:

String userDir = System.getProperty("user.dir"); Path path = Paths.get(userDir); String project = path.getFileName(); 

Источник

Оцените статью