简体   繁体   English

Java:.class文件和java.lang.Class

[英]Java: .class file and java.lang.Class

Is java.lang.Class the same as the actual .class file? java.lang.Class是否与实际的.class文件相同? ie is the content equivalent? 即内容是否等同?

I want to send the .class over socket, and was wondering if instead of trying to find and load the actual .class file, if I could just transmit the java.lang.Class instead? 我想通过套接字发送.class,并想知道如果我只是传输java.lang.Class而不是试图找到并加载实际的.class文件?

Elaboration (read if you want more info) 精化 (如果您想了解更多信息,请阅读)

Suppose I have a Java class called SomeObj. 假设我有一个名为SomeObj的Java类。 When this file is compiled, a file called SomeObj.class will be generated. 编译此文件时,将生成名为SomeObj.class的文件。

We also know that if I have SomeObj as a type, we could get its java.lang.Class type by doing: 我们也知道如果我将SomeObj作为一个类型,我们可以通过执行以下操作来获取其java.lang.Class类型:

Class someObjClss = SomeObj.class;

We know java.lang.Class implements Serializable, thus it can be transmitted. 我们知道java.lang.Class实现了Serializable,因此可以传输。

So is java.lang.Class basically the object representation of the actual .class file? 那么java.lang.Class基本上是实际.class文件的对象表示吗?

UPDATE: 更新:

Assuming I have transmitted the .class file over to another host, do I just use the defineClass() method to construct the class back? 假设我已将.class文件传输到另一台主机,我是否只使用defineClass()方法构建该类?

Link here 链接在这里

UPDATE2: UPDATE2:

This code does returns null InputStream. 此代码确实返回null InputStream。 How is that possible? 怎么可能?

Class clazz = String.class;

String className = clazz.getName(); System.out.println(className);
URL url = clazz.getResource(className);
if( url != null )
{
  String pathName = url.getPath(); System.out.println(className);
}

InputStream inputStream = clazz.getResourceAsStream(className);
if(inputStream != null )
{
  System.out.println(inputStream.available());
}

No. java.lang.Class is a java class. java.lang.Class是java类。 It has its own .class file :-) An instance of java.lang.Class is used to represent class of your object allowing you to perform certain operations (eg use reflection API) on it. 它有自己的.class文件:-) java.lang.Class一个实例用于表示对象的类,允许您对其执行某些操作(例如,使用反射API)。

Serialization has nothing to do with .class file at all - it's object state that gets serialized. 序列化根本与.class文件无关 - 它是被序列化的对象状态 If you were to serialize Class<MyObject> and send that over the wire to a JVM which does not have MyObject.class, it still wouldn't know what MyObject is. 如果您要序列化Class<MyObject>并通过线路将其发送到没有MyObject.class的JVM,它仍然不知道MyObject是什么。

Why do you need to manually send .class over the wire to begin with? 为什么你需要手动发送.class开头? There are remote class loaders to deal with this. 有远程类加载器来处理这个问题。

A java.lang.Class instance is related to a corresponding ".class" file, but they are by no means equivalent. java.lang.Class实例与相应的“.class”文件相关,但它们并不等效。

The java.lang.Class instance encodes the type signature for a ".class" file, but not a lot more. java.lang.Class实例对“.class”文件的类型签名进行编码,但不是很多。 So, though a java.lang.Class can be serialized, doing so does not provide you with enough to allow an instance of the corresponding class to be instantiated at the other end. 因此,尽管可以序列化java.lang.Class ,但这样做并不足以允许在另一端实例化相应类的实例。 If you want to do that, you should be sending the ".class" file. 如果你想这样做,你应该发送“.class”文件。

I think the OP is trying to identify a file on the classpath in which the class file exists. 我认为OP正在尝试在类路径中识别存在类文件的文件。 See http://asolntsev.blogspot.com/2008/03/how-to-find-which-jar-file-contains.html http://asolntsev.blogspot.com/2008/03/how-to-find-which-jar-file-contains.html

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

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