Java:.class 文件和 java.lang.Class
java.lang.Class 与实际的 .class 文件相同吗?即内容是等价的吗?
我想通过套接字发送 .class,并且想知道是否可以只传输 java.lang.Class,而不是尝试查找并加载实际的 .class 文件?
详细说明(如果您想了解更多信息,请阅读)
假设我有一个名为 SomeObj 的 Java 类。编译该文件时,将生成一个名为 SomeObj.class 的文件。
我们还知道,如果我有 SomeObj 作为类型,我们可以通过以下方式获取它的 java.lang.Class 类型:
Class someObjClss = SomeObj.class;
我们知道 java.lang.Class 实现了 Serialized,因此它可以被传输。
那么 java.lang.Class 基本上是实际 .class 文件的对象表示吗?
更新:
假设我已将 .class 文件传输到另一台主机,我是否只使用 DefineClass() 方法来构造回类?
链接此处
UPDATE2 :
此代码确实返回 null InputStream。这怎么可能?
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());
}
Is java.lang.Class the same as the actual .class file? i.e. 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?
Elaboration (read if you want more info)
Suppose I have a Java class called SomeObj. When this file is compiled, a file called SomeObj.class will be generated.
We also know that if I have SomeObj as a type, we could get its java.lang.Class type by doing:
Class someObjClss = SomeObj.class;
We know java.lang.Class implements Serializable, thus it can be transmitted.
So is java.lang.Class basically the object representation of the actual .class file?
UPDATE:
Assuming I have transmitted the .class file over to another host, do I just use the defineClass() method to construct the class back?
Link here
UPDATE2:
This code does returns 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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。
java.lang.Class
是一个 java 类。它有自己的 .class 文件:-)java.lang.Class
的实例用于表示对象的类,允许您对其执行某些操作(例如使用反射API)。序列化与 .class 文件完全无关 - 它是被序列化的对象状态。如果您要序列化
Class
并将其通过网络发送到没有 MyObject.class 的 JVM,它仍然不知道 MyObject 是什么。为什么您需要首先通过网络手动发送.class?有远程类加载器来处理这个问题。
No.
java.lang.Class
is a java class. It has its own .class file :-) An instance ofjava.lang.Class
is used to represent class of your object allowing you to perform certain operations (e.g. use reflection API) on it.Serialization has nothing to do with .class file at all - it's object state that gets serialized. 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.Why do you need to manually send .class over the wire to begin with? There are remote class loaders to deal with this.
java.lang.Class
实例与相应的“.class”文件相关,但它们绝不是等价的。java.lang.Class
实例对“.class”文件的类型签名进行编码,但仅此而已。因此,虽然 java.lang.Class 可以被序列化,但这样做并不能为您提供足够的能力来允许在另一端实例化相应类的实例。如果您想这样做,您应该发送“.class”文件。A
java.lang.Class
instance is related to a corresponding ".class" file, but they are by no means equivalent.The
java.lang.Class
instance encodes the type signature for a ".class" file, but not a lot more. So, though ajava.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. If you want to do that, you should be sending the ".class" file.我认为OP正在尝试识别类路径上存在类文件的文件。请参阅http://asolntsev.blogspot .com/2008/03/how-to-find-which-jar-file-contains.html
I think the OP is trying to identify a file on the classpath in which the class file exists. See http://asolntsev.blogspot.com/2008/03/how-to-find-which-jar-file-contains.html
查看RMI(远程方法调用)。
Take a look at RMI (Remote Method Invocation).