Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ To get Java class / jar package path     - Linux Network Programming - raw socket instance: MAC header message analysis (Programming)

- How to configure Proftpd server on Fedora 22 (Server)

- Applications in Objective-C runtime mechanism (Programming)

- To compile and install Clang 3.5.0 in Linux CentOS (Linux)

- Docker study notes (Server)

- LMMS 1.03 install on Ubuntu 14.04 (Linux)

- Firewall chapter of Linux server security configuration (Linux)

- Install FFmpeg compiling from source in Mac OS X environment (Linux)

- Implement firewall function on a closed Linux machine (Linux)

- System Security: Build Linux with LIDS steel castle (Linux)

- Linux SSH login without a password (Linux)

- How to modify the Ubuntu terminal user name changed to red color (Linux)

- About Samba certification process and permissions (Linux)

- Using a proxy method under Linux terminal (Linux)

- Upgrading to MySQL 5.7 partitioning problem solving (Database)

- To setup a ftp server under Linux (Server)

- JavaScript event handling Detailed (Programming)

- Mac OS X command line to submit the local project to Git (Server)

- Python implementation restart the router (Programming)

- Linux systems dmesg command processing failures and system information collected seven usage (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);
    @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:      
 
- The Samba service does not have permission to access (Server)
- Linux FTP setting file description (Server)
- Github with .gitignore ignore specified file (Linux)
- Compare Swift achieve rapid sorting and sorted Methods (Programming)
- Linux iostat command example explanation (Linux)
- Installation of Python2.7.8 and iPython under CentOS6.5 (Linux)
- Scala REPL Shell call (Programming)
- Ubuntu uses conky add desktop control (Linux)
- dmidecode command Detailed (Hardware information) (Linux)
- Oracle database import and export (Database)
- Android Studio interface-related settings (Linux)
- Linux Shell Scripting Interview Question (Linux)
- Linux Command Tutorial: cat command to view the contents of the file (Linux)
- How to install the latest version of the Eclipse in Ubuntu 14.04 (Linux)
- MongoDB relations, references, index query coverage (Database)
- Python Dir find a folder several files (Programming)
- CentOS7 installation hardware monitoring for Zabbix enterprise applications (Server)
- IP Security Policy is to learn how to prevent Ping and closed ports (Linux)
- Linux installation notes under GAMIT (Linux)
- A well-known but rarely used skills: object pool (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.