質問編集履歴
6
ソースコード変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
23
23
|
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
24
24
|
at java.lang.reflect.Method.invoke(Method.java:498)
|
25
|
-
at versionsmod.versionsmod.init(versionsmod.java:
|
25
|
+
at versionsmod.versionsmod.init(versionsmod.java:57)
|
26
26
|
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
27
27
|
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
28
28
|
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
@@ -63,7 +63,7 @@
|
|
63
63
|
at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)
|
64
64
|
at GradleStart.main(Unknown Source)
|
65
65
|
Caused by: java.lang.NoSuchFieldError: ar
|
66
|
-
at test.test.init(test.java:
|
66
|
+
at test.test.init(test.java:21)
|
67
67
|
... 44 more
|
68
68
|
```
|
69
69
|
test.javaというのは外部Jarの.classのファイルのことです。
|
@@ -72,44 +72,34 @@
|
|
72
72
|
|
73
73
|
versionsmod.java
|
74
74
|
```Java
|
75
|
-
/*
|
75
|
+
/*38行目*/@EventHandler
|
76
76
|
public void init(FMLInitializationEvent e) {
|
77
77
|
File modDir = new File(mod_dir);
|
78
|
+
System.out.println(Block.class);
|
78
79
|
try {
|
79
|
-
for(File f : new File(modDir,"cache").listFiles()) {
|
80
|
-
if(f.getName().substring(f.getName().lastIndexOf(".")).equals(".zip")) {
|
81
|
-
ClassLoader loader = new URLClassLoader(new URL[] {f.toURL()},this.getClass().getClassLoader());
|
82
|
-
ZipFile zip = new ZipFile(f);
|
83
|
-
|
84
|
-
Enumeration<? extends ZipEntry> e1 = zip.entries();
|
85
|
-
|
80
|
+
for(String className : loadedClasses) {
|
86
|
-
ZipEntry entry = (ZipEntry) e1.nextElement();
|
87
|
-
|
81
|
+
if (className.toLowerCase().substring(className.toLowerCase().lastIndexOf(".")).equals(".class")) {
|
88
|
-
|
82
|
+
System.out.println("load "+className.replace("/", ".").replace(".class", ""));
|
89
|
-
|
83
|
+
Class clas = getClass().getClassLoader().loadClass(className.replace("/", ".").replace(".class", ""));
|
90
|
-
|
84
|
+
if(clas.getAnnotation(Mod.class) != null) {
|
91
|
-
|
85
|
+
Mod m = (Mod) clas.getAnnotation(Mod.class);
|
92
|
-
|
86
|
+
Object o = clas.newInstance();
|
93
|
-
|
87
|
+
mods.put(m, o);
|
94
|
-
|
88
|
+
for(Method meth : clas.getDeclaredMethods()) {
|
95
|
-
|
89
|
+
Class[] classes = meth.getParameterTypes();
|
96
|
-
|
90
|
+
if(classes.length==1&&classes[0].getName().equals(FMLInitializationEvent.class.getName())) {
|
97
|
-
|
91
|
+
try {
|
98
|
-
|
92
|
+
meth.setAccessible(true);
|
99
|
-
|
93
|
+
FMLInitializationEvent event = new FMLInitializationEvent();
|
94
|
+
meth.invoke(o, event);
|
100
|
-
|
95
|
+
}catch(InvocationTargetException e11) {
|
101
|
-
|
96
|
+
e11.printStackTrace();
|
102
|
-
}
|
103
|
-
}
|
104
97
|
}
|
105
98
|
}
|
106
99
|
}
|
107
100
|
}
|
108
|
-
zip.close();
|
109
101
|
}
|
110
102
|
}
|
111
|
-
System.out.println(mods.size());
|
112
|
-
System.out.println(Item.class.getClassLoader()+":"+this.getClass().getClassLoader());
|
113
103
|
} catch (Exception e1) {
|
114
104
|
e1.printStackTrace();
|
115
105
|
}
|
@@ -118,17 +108,9 @@
|
|
118
108
|
|
119
109
|
test.java(外部Jarの中に入っているtest.class)
|
120
110
|
```Java
|
121
|
-
/*
|
111
|
+
/*19行目*/@EventHandler
|
122
|
-
public void init(FMLInitializationEvent e)
|
112
|
+
public void init(FMLInitializationEvent e) {
|
123
|
-
for(Field field : Item.class.getFields()) {
|
124
|
-
System.out.println(field.getName());
|
125
|
-
}
|
126
|
-
for(Field field : Block.class.getFields()) {
|
127
|
-
System.out.println(field.getName());
|
128
|
-
}
|
129
|
-
System.out.println(Block.class);
|
130
|
-
System.out.println(Block.gravel);
|
131
|
-
System.out.println(Item.
|
113
|
+
System.out.println(Item.ar);
|
132
114
|
}
|
133
115
|
```
|
134
116
|
|
5
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -116,6 +116,22 @@
|
|
116
116
|
}
|
117
117
|
```
|
118
118
|
|
119
|
+
test.java(外部Jarの中に入っているtest.class)
|
120
|
+
```Java
|
121
|
+
/*13行目*/@EventHandler
|
122
|
+
public void init(FMLInitializationEvent e) throws Exception {
|
123
|
+
for(Field field : Item.class.getFields()) {
|
124
|
+
System.out.println(field.getName());
|
125
|
+
}
|
126
|
+
for(Field field : Block.class.getFields()) {
|
127
|
+
System.out.println(field.getName());
|
128
|
+
}
|
129
|
+
System.out.println(Block.class);
|
130
|
+
System.out.println(Block.gravel);
|
131
|
+
System.out.println(Item.flint);
|
132
|
+
}
|
133
|
+
```
|
134
|
+
|
119
135
|
### 試したこと
|
120
136
|
|
121
137
|
外部Jarの中に以下のソースを書き、実行しました。
|
4
補足情報が別物と勘違いしていたため
title
CHANGED
File without changes
|
body
CHANGED
@@ -66,11 +66,13 @@
|
|
66
66
|
at test.test.init(test.java:17)
|
67
67
|
... 44 more
|
68
68
|
```
|
69
|
+
test.javaというのは外部Jarの.classのファイルのことです。
|
69
70
|
|
70
71
|
### 該当のソースコード
|
71
72
|
|
73
|
+
versionsmod.java
|
72
74
|
```Java
|
73
|
-
@EventHandler
|
75
|
+
/*71行目*/@EventHandler
|
74
76
|
public void init(FMLInitializationEvent e) {
|
75
77
|
File modDir = new File(mod_dir);
|
76
78
|
try {
|
@@ -126,6 +128,6 @@
|
|
126
128
|
しかしinvokeでは見つからないというのがよくわかりません。
|
127
129
|
|
128
130
|
### 補足情報
|
129
|
-
Minecraft Forge 1.
|
131
|
+
Minecraft Forge 1.7.10-10.13.4.1558
|
130
|
-
Eclipse 4.
|
132
|
+
Eclipse 4.4
|
131
133
|
Windows 10 64bit
|
3
書き方の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,67 +1,22 @@
|
|
1
|
+
### 前提・実現したいこと
|
1
2
|
MinecraftというゲームのMODを作っているのですが、そのMODから更に外部のJarファイルを読み込むというものを作っています。
|
3
|
+
プログラムが実行されると、外部のJarを読み込み、さらにその中のプログラムをリフレクションを使用し、実行させるというものです。
|
4
|
+
|
5
|
+
arという変数が見つからないという問題が起きています。
|
6
|
+
|
2
7
|
関係は次のようになっています。
|
3
8
|
Minecraft
|
4
9
|
リフレクションを使用して外部JarのMODを実行
|
5
10
|
私の作っているMODの関数が呼ばれる
|
6
11
|
更にリフレクションを使用して外部のJarを実行(invoke)
|
7
12
|
そこでクラスは見つかるが変数が見つからないというエラーが出る
|
8
|
-
ソースは以下のようになっています。
|
9
|
-
Minecraft実行時にinitが呼ばれます。
|
10
|
-
どうしたら変数を見つけることができますか?
|
11
|
-
関数はしっかりpublic staticとしています。(外部からアクセスできるという説明です)
|
12
|
-
そして、不思議な事に更に外部のJarを実行したときに以下のようなコード
|
13
|
-
```Java
|
14
|
-
for(Field field : Item.class.getFields()) {
|
15
|
-
System.out.println(field.getName());
|
16
|
-
}
|
17
|
-
```
|
18
|
-
を実行すると、arが存在することが確認できました。しかしinvokeした時に見つからないというのが不思議です。
|
19
|
-
```Java
|
20
|
-
@EventHandler
|
21
|
-
public void init(FMLInitializationEvent e) {
|
22
|
-
File modDir = new File(mod_dir);
|
23
|
-
try {
|
24
|
-
for(File f : new File(modDir,"cache").listFiles()) {
|
25
|
-
if(f.getName().substring(f.getName().lastIndexOf(".")).equals(".zip")) {
|
26
|
-
ClassLoader loader = new URLClassLoader(new URL[] {f.toURL()},this.getClass().getClassLoader());
|
27
|
-
ZipFile zip = new ZipFile(f);
|
28
13
|
|
29
|
-
Enumeration<? extends ZipEntry> e1 = zip.entries();
|
30
|
-
|
14
|
+
invokeしたプログラムでは変数を書き換えることはありません。
|
31
|
-
|
15
|
+
|
32
|
-
if (!entry.isDirectory()&&entry.getName().toLowerCase().substring(entry.getName().toLowerCase().lastIndexOf(".")).equals(".class")) {
|
33
|
-
System.out.println("load "+entry.getName().replace("/", ".").replace(".class", ""));
|
34
|
-
Class clas = Class.forName(entry.getName().replace("/", ".").replace(".class", ""),false,loader);
|
35
|
-
if(clas.getAnnotation(Mod.class) != null) {
|
36
|
-
Mod m = (Mod) clas.getAnnotation(Mod.class);
|
37
|
-
Object o = clas.newInstance();
|
38
|
-
mods.put(m, o);
|
39
|
-
for(Method meth : clas.getDeclaredMethods()) {
|
40
|
-
Class[] classes = meth.getParameterTypes();
|
41
|
-
if(classes.length==1) if(classes[0].getName().equals(FMLInitializationEvent.class.getName())) {
|
42
|
-
try {
|
43
|
-
meth.setAccessible(true);
|
44
|
-
meth.invoke(o, new FMLInitializationEvent());//java.lang.reflect.InvocationTargetExceptionエラー
|
45
|
-
}catch(InvocationTargetException e11) {
|
46
|
-
e11.printStackTrace();
|
47
|
-
}
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|
52
|
-
}
|
53
|
-
zip.close();
|
54
|
-
}
|
55
|
-
}
|
56
|
-
System.out.println(mods.size());
|
57
|
-
System.out.println(Item.class.getClassLoader()+":"+this.getClass().getClassLoader());
|
58
|
-
} catch (Exception e1) {
|
59
|
-
|
16
|
+
### 発生している問題・エラーメッセージ
|
17
|
+
|
60
|
-
|
18
|
+
arという変数が見つからないエラーが出てしまいます。
|
61
|
-
}
|
62
19
|
```
|
63
|
-
エラー
|
64
|
-
```
|
65
20
|
java.lang.reflect.InvocationTargetException
|
66
21
|
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
67
22
|
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
@@ -111,4 +66,66 @@
|
|
111
66
|
at test.test.init(test.java:17)
|
112
67
|
... 44 more
|
113
68
|
```
|
69
|
+
|
70
|
+
### 該当のソースコード
|
71
|
+
|
72
|
+
```Java
|
73
|
+
@EventHandler
|
74
|
+
public void init(FMLInitializationEvent e) {
|
75
|
+
File modDir = new File(mod_dir);
|
76
|
+
try {
|
77
|
+
for(File f : new File(modDir,"cache").listFiles()) {
|
78
|
+
if(f.getName().substring(f.getName().lastIndexOf(".")).equals(".zip")) {
|
79
|
+
ClassLoader loader = new URLClassLoader(new URL[] {f.toURL()},this.getClass().getClassLoader());
|
80
|
+
ZipFile zip = new ZipFile(f);
|
81
|
+
|
82
|
+
Enumeration<? extends ZipEntry> e1 = zip.entries();
|
83
|
+
while (e1.hasMoreElements()) {
|
84
|
+
ZipEntry entry = (ZipEntry) e1.nextElement();
|
85
|
+
if (!entry.isDirectory()&&entry.getName().toLowerCase().substring(entry.getName().toLowerCase().lastIndexOf(".")).equals(".class")) {
|
86
|
+
System.out.println("load "+entry.getName().replace("/", ".").replace(".class", ""));
|
87
|
+
Class clas = Class.forName(entry.getName().replace("/", ".").replace(".class", ""),false,loader);
|
88
|
+
if(clas.getAnnotation(Mod.class) != null) {
|
89
|
+
Mod m = (Mod) clas.getAnnotation(Mod.class);
|
90
|
+
Object o = clas.newInstance();
|
91
|
+
mods.put(m, o);
|
92
|
+
for(Method meth : clas.getDeclaredMethods()) {
|
93
|
+
Class[] classes = meth.getParameterTypes();
|
94
|
+
if(classes.length==1) if(classes[0].getName().equals(FMLInitializationEvent.class.getName())) {
|
95
|
+
try {
|
96
|
+
meth.setAccessible(true);
|
97
|
+
meth.invoke(o, new FMLInitializationEvent());//java.lang.reflect.InvocationTargetExceptionエラー
|
98
|
+
}catch(InvocationTargetException e11) {
|
99
|
+
e11.printStackTrace();
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
zip.close();
|
107
|
+
}
|
108
|
+
}
|
109
|
+
System.out.println(mods.size());
|
110
|
+
System.out.println(Item.class.getClassLoader()+":"+this.getClass().getClassLoader());
|
111
|
+
} catch (Exception e1) {
|
112
|
+
e1.printStackTrace();
|
113
|
+
}
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
### 試したこと
|
118
|
+
|
119
|
+
外部Jarの中に以下のソースを書き、実行しました。
|
120
|
+
```Java
|
121
|
+
for(Field field : Item.class.getFields()) {
|
122
|
+
System.out.println(field.getName());
|
123
|
+
}
|
124
|
+
```
|
125
|
+
するとarというフィールドが存在するというのがわかりました。
|
114
|
-
|
126
|
+
しかしinvokeでは見つからないというのがよくわかりません。
|
127
|
+
|
128
|
+
### 補足情報
|
129
|
+
Minecraft Forge 1.6.4-9.11.1.965
|
130
|
+
Eclipse 4.3
|
131
|
+
Windows 10 64bit
|
2
より詳しい説明の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,19 +3,19 @@
|
|
3
3
|
Minecraft
|
4
4
|
リフレクションを使用して外部JarのMODを実行
|
5
5
|
私の作っているMODの関数が呼ばれる
|
6
|
-
更にリフレクションを使用して外部のJarを実行
|
6
|
+
更にリフレクションを使用して外部のJarを実行(invoke)
|
7
7
|
そこでクラスは見つかるが変数が見つからないというエラーが出る
|
8
8
|
ソースは以下のようになっています。
|
9
9
|
Minecraft実行時にinitが呼ばれます。
|
10
10
|
どうしたら変数を見つけることができますか?
|
11
|
-
関数はしっかりpublic staticとしています。
|
11
|
+
関数はしっかりpublic staticとしています。(外部からアクセスできるという説明です)
|
12
12
|
そして、不思議な事に更に外部のJarを実行したときに以下のようなコード
|
13
13
|
```Java
|
14
14
|
for(Field field : Item.class.getFields()) {
|
15
15
|
System.out.println(field.getName());
|
16
16
|
}
|
17
17
|
```
|
18
|
-
を実行すると、arが存在することが確認できました。
|
18
|
+
を実行すると、arが存在することが確認できました。しかしinvokeした時に見つからないというのが不思議です。
|
19
19
|
```Java
|
20
20
|
@EventHandler
|
21
21
|
public void init(FMLInitializationEvent e) {
|
@@ -110,4 +110,5 @@
|
|
110
110
|
Caused by: java.lang.NoSuchFieldError: ar
|
111
111
|
at test.test.init(test.java:17)
|
112
112
|
... 44 more
|
113
|
-
```
|
113
|
+
```
|
114
|
+
invokeしたプログラムでは変数を書き換えることはありません。
|
1
エラー文の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,7 +9,14 @@
|
|
9
9
|
Minecraft実行時にinitが呼ばれます。
|
10
10
|
どうしたら変数を見つけることができますか?
|
11
11
|
関数はしっかりpublic staticとしています。
|
12
|
+
そして、不思議な事に更に外部のJarを実行したときに以下のようなコード
|
12
13
|
```Java
|
14
|
+
for(Field field : Item.class.getFields()) {
|
15
|
+
System.out.println(field.getName());
|
16
|
+
}
|
17
|
+
```
|
18
|
+
を実行すると、arが存在することが確認できました。
|
19
|
+
```Java
|
13
20
|
@EventHandler
|
14
21
|
public void init(FMLInitializationEvent e) {
|
15
22
|
File modDir = new File(mod_dir);
|
@@ -52,4 +59,55 @@
|
|
52
59
|
e1.printStackTrace();
|
53
60
|
}
|
54
61
|
}
|
62
|
+
```
|
63
|
+
エラー
|
64
|
+
```
|
65
|
+
java.lang.reflect.InvocationTargetException
|
66
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
67
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
68
|
+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
69
|
+
at java.lang.reflect.Method.invoke(Method.java:498)
|
70
|
+
at versionsmod.versionsmod.init(versionsmod.java:73)
|
71
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
72
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
73
|
+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
74
|
+
at java.lang.reflect.Method.invoke(Method.java:498)
|
75
|
+
at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:532)
|
76
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
77
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
78
|
+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
79
|
+
at java.lang.reflect.Method.invoke(Method.java:498)
|
80
|
+
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
|
81
|
+
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
|
82
|
+
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
|
83
|
+
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
|
84
|
+
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
|
85
|
+
at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:212)
|
86
|
+
at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:190)
|
87
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
88
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
89
|
+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
90
|
+
at java.lang.reflect.Method.invoke(Method.java:498)
|
91
|
+
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
|
92
|
+
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
|
93
|
+
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
|
94
|
+
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
|
95
|
+
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
|
96
|
+
at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119)
|
97
|
+
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:737)
|
98
|
+
at cpw.mods.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:311)
|
99
|
+
at net.minecraft.client.Minecraft.startGame(Minecraft.java:597)
|
100
|
+
at net.minecraft.client.Minecraft.run(Minecraft.java:942)
|
101
|
+
at net.minecraft.client.main.Main.main(Main.java:164)
|
102
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
103
|
+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|
104
|
+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|
105
|
+
at java.lang.reflect.Method.invoke(Method.java:498)
|
106
|
+
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
|
107
|
+
at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
|
108
|
+
at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)
|
109
|
+
at GradleStart.main(Unknown Source)
|
110
|
+
Caused by: java.lang.NoSuchFieldError: ar
|
111
|
+
at test.test.init(test.java:17)
|
112
|
+
... 44 more
|
55
113
|
```
|