Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ To get Java class / jar package path     - Linux log management tools Logrotate (Linux)

- Linux using RAID how to use Mdadm Tool Management Software RAID (Linux)

- Vmstat command Linux Performance Monitoring (Linux)

- Linux NFS FTP use (Server)

- Linux filtration empty file command summary (Linux)

- Configuring a Linux operating system against syn attack (Linux)

- Recent Consolidation Using Linux security module (Linux)

- Docker: installation under Ubuntu (Server)

- Compare Several MySQL environmental issues (Database)

- Scope of variables in C # (Programming)

- According to the national position on how to block traffic in Linux (Server)

- Linux ban single-user mode to enhance system security (Linux)

- for (int a: i) the use of the Java programming (Programming)

- About Samba certification process and permissions (Linux)

- Linux security-related basics (Linux)

- Linux User Management (Linux)

- Ubuntu install Eclipse for C / C ++ and related configuration (Linux)

- RHEL7.0 log system (Linux)

- Linux Monitoring Command (Linux)

- Ubuntu 15.04 / 14.04 install Ubuntu After Install 2.6 (Linux)

  To get Java class / jar package path
  Add Date : 2017-04-13      
  In the Java platform, occasionally encountered because of conflicts Class while being given method does not exist such problems, but compile time and no problems. In the same environment to compile and run, this is generally not the case; in a compiled environment, but run in another environment, such as integrated environment upgrade, you may experience this problem. It may be due to inconsistencies in the jar integrated environment and development environments, such as more or less of the jar, it is leading to the source of the conflict.

Imagine if in a given place, may get the path of the class where the jar or packet path, you can navigate to the class file, and confirm that we are not used by the compiler when the class file.

In general, we can use as ClassPathTest.class.getResource ( "") getPath ();. The way to get the position ClassPathTest class corresponding class file, even if it is in a jar package; but the use of this approach for getting jar class package path, not a panacea, some jar package adds security, can never acquired.

We can also use LogFactory.class.getProtectionDomain () getCodeSource () getLocation () getFile ();... The way to get LogFactory the class currently in use where the jar package path, as compared to the previous way, this the class is designed for the jar, and the only path to the jar wrapped in a layer structure, the package does not contain its inner structure. But the same is not a panacea for being secured jar, the same can not be obtained. As java.lang.System, we can not know its path information.

package cn.j2se.junit.classpath;

import static org.junit.Assert *.;

import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

public class ClassPathTest {
    private static final Log logger = LogFactory.getLog (ClassPathTest.class);
    public void testClassPath () {
        String classPath = ClassPathTest.class.getResource ( "") getPath ().;
        logger.info ( "the classpath of ClassPathTest is: [" + classPath + "]");
        assertEquals (1, 1);
    public void testJarPath () throws UnsupportedEncodingException {
        // System.class.getResource ( "") == null
        String lfClassPath = LogFactory.class.getResource ( "") getPath ().;
        logger.info ( "the classpath of LogFactory is: [" + lfClassPath + "]");
        // System.class.getProtectionDomain (). GetCodeSource () == null
        String lfJarPath = LogFactory.class.getProtectionDomain () getCodeSource () getLocation () getFile ()...;
        logger.info ( "the jar path of LogFactory is: [" + lfJarPath + "]");
        assertEquals (1, 1);


[INFO] 2015-08-25 14:26:30 CST
        the classpath of ClassPathTest is: [/ D: / develop / eclipse_jee_juno_SR2 / workspace / lijinlong / j2se / bin / cn / j2se / junit / classpath /]

[INFO] 2015-08-25 14:26:30 CST
        the classpath of LogFactory is: [file: / D:! /develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar / org / apache / commons / logging /]

[INFO] 2015-08-25 14:26:30 CST
        the jar path of LogFactory is: [/ D: /develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar]
- C ++, overloading, cover, hide (Programming)
- Five Linux user space debugging tool (Linux)
- Vim (Linux)
- C ++: Postmodern systems programming language (Programming)
- Linux uses a screen recording under RecordMyDesktop (Linux)
- Linux, how to filter, split, and merge pcap file (Linux)
- Caffe install under Ubuntu 14.04 64bit (Linux)
- Python: Finding meet the conditions specified in the file directory (Programming)
- Use Hexo quickly build and deploy a blog to Github (Server)
- File encryption and decryption of Linux security mechanisms (Linux)
- Hadoop 2.7.1 Installation and Configuration under RedHat Linux 6.5 (Server)
- Git remote repository Explanation (Linux)
- CentOS 6.7 compile and install LAMP (Server)
- Ubuntu How to mount iso file (Linux)
- Install Redis 2.6 5.5 32 position CentOS error resolved (Linux)
- iostat command Detailed (Linux)
- Role Object of registerNatives () method (Programming)
- To configure parameter configuration and software installation and uninstallation under Linux (Linux)
- Shell array: Define Shell array, the array length (Programming)
- Ubuntu and derived versions of the user how to install G Mic (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.