Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ To get Java class / jar package path     - How to use Git to upload code to GitHub project (Linux)

- Mounting kit under Fedora Linux (Linux)

- Django template inheritance child (Programming)

- Python 2.7 installed on CentOS 6.5 (Linux)

- Java regular expressions examples (Programming)

- Let the router be your security barrier against external attack (Linux)

- HTTP Client Hints Introduction (Server)

- Download Google Android source code under Ubuntu 4.4 (Linux)

- Use Redis as time-series database: why and how (Database)

- C + + secondary pointer memory model (pointer array) (Programming)

- Zabbix configure DataGuard monitoring (Database)

- How to install Ubuntu applications Device 7 (Linux)

- Detailed driver compiled into the Linux kernel (Programming)

- ORA-30926 and MERGE tables empty the temporary occupation problem (Database)

- A well-known but rarely used skills: object pool (Programming)

- Android Studio quick overview of Gradle (Programming)

- Server Security Analysis attack on Linux (Linux)

- Import and export myloader accelerate mydumper (Database)

- Apache Kafka: the next generation of distributed messaging system (Server)

- Use JMS listener Oracle AQ, trigger the execution of Java programs in the database changes (Database)

 
         
  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);
    @Test
    public void testClassPath () {
        String classPath = ClassPathTest.class.getResource ( "") getPath ().;
        logger.info ( "the classpath of ClassPathTest is: [" + classPath + "]");
        
        assertEquals (1, 1);
    }
    
    @Test
    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]
     
         
         
         
  More:      
 
- Install VLC player on Ubuntu 14.10 (Linux)
- How to view the Linux program or process used in the library (Linux)
- VMware virtual machine to use bridged mode fast Internet access (Linux)
- JavaScript basic tools list (Programming)
- C language programming entry - Common operators (Programming)
- Oracle 11g forget approach SYS and SYSTEM password (Database)
- Analysis examples: Intrusion Response Linux platform Case (Linux)
- Android basics summary article (Programming)
- LVM management reduces swap partition space to the root partition (Linux)
- Linux iptables: combat scenes (Linux)
- Relationship between Linux permissions with the command (Linux)
- Java-- get the reflection object information (Programming)
- Ubuntu 14.04 install PostgreSQL 9.2 (Database)
- Java reflection Introduction (Programming)
- Ubuntu uses the / etc / profile file to configure the JAVA environment variable (Linux)
- Linux User Management (Linux)
- How to build a container cluster (Server)
- How to Install Node.js in CentOS 7 (Linux)
- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)
- findmnt- Looking mounted file system (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.