简体   繁体   English

在Java中查找资源时,两个类在相同的代码上提供不同的输出

[英]Two classes give the output different on same code while locating a resource in java

i have two classes as 我有两个班

public class CsvReaderExample { 
    public static void main(String[] args) {
        try {               
    String file=CsvReaderExample.class.getResource("/saralbitta/common/db/DateNepali.csv").getFile();

Next class is 下一堂课是

 public class LoanIssueView extends ViewPart {
    public LoanIssueView() {    
            }
            @Override
   public void createPartControl(Composite parent) {
   String file=LoanIssueView.class.getResource("/saralbitta/common/db/DateNepali.csv").getFile();

When i print file in both the classes i am so surprised that in one class i get the outPut as below in first case 当我在两个班级中都打印file ,我感到非常惊讶,以至于在一个班级中我得到如下所示的outPut

 /E:/myskbbl/saralbitta/bin/saralbitta/common/db/DateNepali.csv 

similarly in second case i get outPut as 同样在第二种情况下我得到outPut作为

/saralbitta/common/db/DateNepali.csv

Why the two classes with same code give different output. 为什么两个具有相同代码的类给出不同的输出。 i am getting fileNotFoundException in second case as i need the output like in first case. 我在第二种情况下收到fileNotFoundException,因为我需要像第一种情况下的输出。 How can i obtain the like the output like first case in the second one? 我如何在第二种情况下获得类似第一种情况的输出?

Are the classes CsvReaderExample and LoanIssueView contained in different bundles/plugins? CsvReaderExample和LoanIssueView类是否包含在不同的包/插件中? Each bundle has its own classpath and cannot see the classes of other bundles, unless you allow it. 每个捆绑软件都有其自己的类路径,除非您允许,否则无法查看其他捆绑软件的类。 Interbundle dependencies are declared in the Manifest. 束间依赖关系在清单中声明。

Edit: Ok then it's clear. 编辑:好的,那就很清楚了。 In the first case OSGi is not started, so it's a normal Java application. 在第一种情况下,OSGi没有启动,因此它是普通的Java应用程序。 In the second case OSGi is started and each bundle gets its own classloader. 在第二种情况下,启动了OSGi,每个捆绑包都有自己的类加载器。 I would try to request the Resource from the bundle. 我会尝试从捆绑包中请求资源。 Use the getResource() method there http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html#getResource%28java.lang.String%29 . http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html#getResource%28java.lang.String%29处使用getResource()方法。 You can acquire a bundle reference from the BundleContext . 您可以从BundleContext获取捆绑参考。 You get the BundleContext from the BundleActivator start() method http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleActivator.html#start%28org.osgi.framework.BundleContext%29 . 您可以从BundleActivator start()方法http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleActivator.html#start%28org.osgi.framework.BundleContext%29获得BundleContext Note that the URL you acquire, is not a File URL. 请注意,您获取的URL不是文件URL。 Use FileLocator to convert the URL to a File URL http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore%2Fruntime%2FFileLocator.html . 使用FileLocator将URL转换为文件URL http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore%2Fruntime %2FFileLocator.html

Where are these programs executed from? 这些程序从哪里执行? When the resource is looked up, there is an algorithm by which the absolute name is determined. 查找资源时,会使用一种算法来确定绝对名称。 It also depends on the class loader. 它还取决于类加载器。 Probably that is why you see different results. 也许这就是为什么您看到不同结果的原因。

It should be related to the difference in classpath setting. 它应该与类路径设置的差异有关。 Check the classpath setting for both apps. 检查两个应用程序的类路径设置。

Check the documentation for both getResource and findResource 查看有关getResourcefindResource的文档

In first case, getResource() is returning you the file string. 在第一种情况下, getResource()返回文件字符串。 But in second case, somehow the getResource() is not able to find the path and tries to invoke findResource() method, as stated in it's documentation: 但是在第二种情况下, getResource()不能以某种方式找到路径, findResource()尝试调用findResource()方法,如其文档所述:

getResource : getResource

This method will first search the parent class loader for the resource; 此方法将首先在父类加载器中搜索资源; if the parent is null the path of the class loader built-in to the virtual machine is searched. 如果父级为null,则搜索虚拟机内置的类加载器的路径。 That failing, this method will invoke findResource(String) to find the resource. 失败的话,此方法将调用findResource(String)来查找资源。

Probably this might be reason here for different return strings, as different methods might be getting called depending on the flow. 这可能是这里使用不同返回字符串的原因,因为可能会根据流程调用不同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM