Get the absolute path of this class in a Java program (Myeclipse,idea)

Source: Internet
Author: User

When you write a certificate request, you encounter the need to go under the absolute path to find the certificate


The absolute path of the current class in MyEclipse is thus obtained:


Package Fafa;


public class Test {
private static String Currentdir = null;
public static void Main (string[] args) {
Currentdir = Test.class.getResource (""). GetPath ();
if (!currentdir.endswith ("/")) {
Currentdir + = "/";
}
System.out.println (Currentdir);
}
}


In idea, the result of this is not the result of the current class, but the path of the current class through the compiled paths, that is to say, the target/classes

How do you get the absolute path of the current class in idea?

The following method can satisfy the demand,

To get the path of the current class is simply to read the file, we can do this:

InputStream fikeyfile = CryptNoRestrict.class.getResourceAsStream ("/" +currentdir);
The Currentdir in the code is the name of the file, for example: text.pfx

This allows you to look directly into the file and then convert it to the form of the stream

As for how to get when the class path is not loaded, temporarily not clear, next supplement.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.