Get java package path

How can i Get file path in some java package?

Java Package http://img250.yukle.tc/images/3464package.jpg Solution 1: If you need the path for reading the file’s contents, it would be simpler (and safer) to use and avoid having to deal with paths entirely. Solution 1: If you create a using which is the , you cannot get the absolute path of the file using , because the relative path is build according to the default user home directory or the JVM path.

How can i Get file path in some java package?

i am new at java so maybe this will be a silly question. I got a file in my jasper.deneme package. I wanna give a full path for it.As ya see in picture i write it like

string fullpath = "/jasper.deneme/reportDeneme.jasper"; 

Java Package http://img250.yukle.tc/images/3464package.jpg

denemeJasper.class. getResource («reportDeneme.jasper»). getPath ()

If you need the path for reading the file’s contents, it would be simpler (and safer) to use getResourceAsStream() and avoid having to deal with paths entirely.

BTW, your class denemeJasper violates ubiquitous Java naming standards; as a class, its name should start with an uppercase letter.

The might be better off with a relative path , but the absolute path would be:

String fullpath = "/jasper/deneme/reportDeneme.jasper"; 

‘/’ replaces ‘.’ in the package name whether the resource is located in a file, a jar or by any other means.

Читайте также:  Успешное выполнение запроса php

Windows — Java — get «program files» path, How can I get the current computer’s «Program Files» path with Java? Stack Overflow. About; Products For Teams; Stack Overflow How can I get the current computer’s «Program Files» path with Java? java windows path. Share. Follow asked Jan 31, 2011 at 13:27.

Get real path of a file in a Java class

String myFile = "D:/dev/workspace/MyProject/WebContent/stats/stats.csv"; File statsFile = new File(myFile); 

But I want to only have the relative path as stats/stats.csv . I don’t want to have to write the complete path in my code.

In a servlet, I do it this way :

File statsFile = new File(this.getServletContext().getRealPath("/") + "stats/stats.csv"); 

But here it is not in a servlet. So what is the equivalent way in a java class ?

You should put it in the CLASSPATH and read it from an InputStream acquired using getResourceAsStream() . It’s the path-independent way to access a file.

String basePath = "D:/dev/workspace/MyProject/WebContent"; String absolutePath = "D:/dev/workspace/MyProject/WebContent/stats/stats.csv"; String relativePath = new File(basePath).toURI(). relativize(new File(absolutePath).toURI()).getPath(); System.out.println(relativePath); // stats/stats.csv 

basePath is the path, relative to which you want to get Relative Path .

I found a solution. I can use my JSP to use File statsFile = new File(this.getServletContext().getRealPath(«/») + «stats/stats.csv») and then pass statsFile to my class.

Java, get full path of file and removing filename, To get the absolute path to the parent directory you can do: File f = new File («C:\\Users\\User\\Desktop\\test\\test.txt»); String path = f.getParentFile ().getAbsolutePath (); System.out.println (path); Output: C:\Users\User\Desktop\test. If you really want the trailing slash, then you can …

Get a file path

Is there a way to get the full path for a file exists on the computer ? For example , I want to get the full path for a file in a folder on desktop

 File f = new File("help.chm"); String f2=f.getAbsolutePath(); f3=f3.replaceAll("\\\\","/" ); System.out.println("Path:"+f3); 

but it gave me the path of the project like this:

although the file is not located there .

If you create a file using new File(«filename») which is the relative path , you cannot get the absolute path of the file using file.getAbsolutePath() , because the relative path is build according to the default user home directory or the JVM path.

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes.

A relative pathname , in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory . This directory is named by the system property user.dir , and is typically the directory in which the Java virtual machine was invoked.

So, to get the absolute path for this case, you would actually have to write the path yourself. Get the absolute path till the directory where you saved the file , and append the file name to it.

Since the other answers do not cover your question, here is my comment:

To get a file’s path, you first need to tell your java program where it is or how to find it.

For your specific example you can get the desktop path using: System.getProperty(«user.home») + «/Desktop»; then you can search through folders on your desktop for a matching file name.

Read here to learn how to search for files: docs.oracle.com/javase/tutorial/essential/io/find.html

A File is a representation of a file path , not necessarily an existent file on disk — ie the file doesn’t have to exist on disk for a File object to be not null .

That’s why there’s the File.exists() method.

The path «help.chm» will be relative to the directory from which you started the JVM, which in your case appears to be C:/Users/toshiba/Documents/NetBeansProjects/test/

To get a path to the desktop , you need to use the absolute path of the desktop directory in Windows, which will be something along the lines of C:/Users/toshiba/Desktop/help.chm

How to get the current working directory in Java?, @indyaah infact this answer is wrong, there is a subtle difference between a user-working-directory and a current-working-directory of a system process (cwd); most of time the «user.dir» points to the cwd of a (java)process; but «user.dir» has different semantics and shall not be used to obtain the cwd of a java process; …

Источник

How to find physical path of current java class using java code.

send pies

posted 15 years ago

  • Report post to moderator
  • I am creating a java class & packaging it into jar. Now I want to add some code into my java class so that it will print its current physical path in file system (either jars path in which it is packaged or java files path if its not packaged in jar) on console.

    ********Deserve Before You Desire********

    send pies

    posted 15 years ago

  • Report post to moderator
  • Sorry bout the irrelevant comment, I am in the middle of a project — I use these answer question programs as handy utiilty code.

    «The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature.»

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • Hm, that seems like a convoluted (and dysfunctional) way to get the current working directory. As simpler way that actually works would be

    However, that’s not what Prashant asked. He’s apparently looking for the location of the class file. He’s a way that can be found (assuming the current class is called Test.class):

    [ February 07, 2008: Message edited by: Jim Yingst ]

    «I’m not back.» — Bill Harding, Twister

    Sheriff

    Chrome

    send pies

    posted 15 years ago

  • Report post to moderator
  • That won’t work if the JAR file is run from another location. E.g.:

    It would print C:\\, ot C:\\SomeFolder.

    There is a trick that works with MOST class loaders; not all though. It is based on the idea that a .class file itself can mostly also be treated as a resource:

    Some notes:
    1) getShortName returns the class name without the package.
    2) you can try to convert the URL to a File using «new File(url.toURI())»

    This works (as tested) with both URLClassLoader and the system class loader.

    SCJP 1.4 — SCJP 6 — SCWCD 5 — OCEEJBD 6 — OCEJPAD 6
    How To Ask Questions How To Answer Questions

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • [Rob]: That won’t work if the JAR file is run from another location.

    Hm, I think my error was more general than that — I included an extra getClassLoader() call that made it only work for classes in the default package. The Class remembers the relevant package, and looks in appropriate subdirectorries, but the ClassLoader does not. I edited my above post to omit that extra call now. The above code works fine on my system even for a jar file in another directory — e.g.

    As for parsing the result, well that’s up to the user what form they want it in; I didn’t really care much about the details of that.
    [ February 07, 2008: Message edited by: Jim Yingst ]

    «I’m not back.» — Bill Harding, Twister

    Sheriff

    Chrome

    send pies

    posted 15 years ago

  • Report post to moderator
  • Originally posted by Jim Yingst:
    [Rob]: That won’t work if the JAR file is run from another location.

    I was referring to Nicholas’ solution; your solution was a simplified version of mine, which definitely works.

    As long as you don’t use some custom class loader that returns something completely different when calling getResource() that is. Fortunately, the default class loaders available in Java do not.

    SCJP 1.4 — SCJP 6 — SCWCD 5 — OCEEJBD 6 — OCEJPAD 6
    How To Ask Questions How To Answer Questions

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • [Rob’]: I was referring to Nicholas’ solution; your solution was a simplified version of mine, which definitely works.

    No, your solution is a complexified version of mine.
    [ February 08, 2008: Message edited by: Jim Yingst ]

    «I’m not back.» — Bill Harding, Twister

    send pies

    posted 15 years ago

  • Report post to moderator
  • Originally posted by Jim Yingst:
    [Rob’]: I was referring to Nicholas’ solution; your solution was a simplified version of mine, which definitely works.

    Well was I dysfunctional)? —

    «The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature.»

    Sheriff

    Chrome

    send pies

    posted 15 years ago

  • Report post to moderator
  • Your solution only works if you are in the path where the JAR file or package folder is located, since it prints out the current working directory. And as Jim has shown, you don’t need a separate class for it; just using new File(«.») works just as well.

    SCJP 1.4 — SCJP 6 — SCWCD 5 — OCEEJBD 6 — OCEJPAD 6
    How To Ask Questions How To Answer Questions

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • Nick’s code doesn’t work at all. It just prints «.». With minor tweaking, it could print the current user directory, but that’s not what the original poster asked for, is it?

    «I’m not back.» — Bill Harding, Twister

    send pies

    posted 15 years ago

  • Report post to moderator
  • Originally posted by Jim Yingst:
    Nick’s code doesn’t work at all.

    I have used it several times — it works so reliably that I do not need to defend my dysfuctional() . it works, see java.io.File() line 183: public File(String pathname). It may be different on your shell which is designed to resemble the unice, but on winnie ‘.’ resolves to current directory. You can do super(«.»); on an extends java.io.File and if(isDirectory()) evaluates to true. datfunctonal() ( your shell ) may not work but dysfunctional() does work.
    [ February 10, 2008: Message edited by: Nicholas Jordan ]

    «The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature.»

    Wanderer

    send pies

    posted 15 years ago

  • Report post to moderator
  • Nick, try running the program you posted. I don’t care if you’ve run a similar program — it’s obvious that you haven’t run the one that you actually posted. This has nothing to do with shells or operating systems. Your program is equally useless on any system.

    «I’m not back.» — Bill Harding, Twister

    Источник

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