[英]Code Compiles in Eclipse but not through command prompt
以下代碼使用JDK7在Eclipse中編譯沒有任何問題(我使用的是更新10,但應該可以與任何版本的JDK7一起使用),但是在通過命令行使用完全相同的JDK進行編譯時會失敗。 該類只提供接口方法的存根實現。
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
public class TestBundle implements Bundle {
/**
* @param args
*/
public static void main(String[] args) {
}
@Override
public int compareTo(Bundle o) {
return 0;
}
@Override
public <A> A adapt(Class<A> arg0) {
return null;
}
@Override
public Enumeration<URL> findEntries(String arg0, String arg1, boolean arg2) {
return null;
}
@Override
public BundleContext getBundleContext() {
return null;
}
@Override
public long getBundleId() {
return 0;
}
@Override
public File getDataFile(String arg0) {
return null;
}
@Override
public URL getEntry(String arg0) {
return null;
}
@Override
public Enumeration<String> getEntryPaths(String arg0) {
return null;
}
@Override
public Dictionary<String, String> getHeaders() {
return null;
}
@Override
public Dictionary<String, String> getHeaders(String arg0) {
return null;
}
@Override
public long getLastModified() {
return 0;
}
@Override
public String getLocation() {
return null;
}
@Override
public ServiceReference<?>[] getRegisteredServices() {
return null;
}
@Override
public URL getResource(String arg0) {
return null;
}
@Override
public Enumeration<URL> getResources(String arg0) throws IOException {
return null;
}
@Override
public ServiceReference<?>[] getServicesInUse() {
return null;
}
@Override
public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(
int arg0) {
return null;
}
@Override
public int getState() {
return 0;
}
@Override
public String getSymbolicName() {
return null;
}
@Override
public Version getVersion() {
return null;
}
@Override
public boolean hasPermission(Object arg0) {
return false;
}
@Override
public Class<?> loadClass(String arg0) throws ClassNotFoundException {
return null;
}
@Override
public void start() throws BundleException {
}
@Override
public void start(int arg0) throws BundleException {
}
@Override
public void stop() throws BundleException {
}
@Override
public void stop(int arg0) throws BundleException {
}
@Override
public void uninstall() throws BundleException {
}
@Override
public void update() throws BundleException {
}
@Override
public void update(InputStream arg0) throws BundleException {
}
}
osgi的jar文件可以從這里下載
我使用以下命令通過命令行編譯它:
javac.exe -cp org.eclipse.osgi_3.8.0.v20120529-1548.jar TestBundle.java
通過命令行編譯時出現以下錯誤:
TestBundle.java:101: error: type ServiceReference does not take parameters
public ServiceReference<?>[] getRegisteredServices() {
^
TestBundle.java:119: error: type ServiceReference does not take parameters
public ServiceReference<?>[] getServicesInUse() {
^
TestBundle.java:18: error: TestBundle is not abstract and does not override abstract method adapt(Class) in Bundle
public class TestBundle implements Bundle {
^
TestBundle.java:28: error: method does not override or implement a method from a supertype
@Override
^
TestBundle.java:35: error: name clash: <A>adapt(Class<A>) in TestBundle and adapt(Class) in Bundle have the same erasure, yet neither overrides the other
public <A> A adapt(Class<A> arg0) {
^
where A is a type-variable:
A extends Object declared in method <A>adapt(Class<A>)
TestBundle.java:34: error: method does not override or implement a method from a supertype
@Override
^
6 errors
在Java 7上編譯時,OSGi規范捆綁包存在泛型問題。這是因為編譯時使用jdk 1.4向后兼容的捆綁包使得它們在Jdk 7中中斷。經過大量投訴后發布了符合以下規定的新版本jdk 7現在。
4.3.1源與4.3.0相同。 它只是重新編譯。 您應該能夠使用此jar編譯代碼。 我不確定這與你使用的eclipse有什么關系,但我猜他們只是使用舊的編譯規范類。
http://search.maven.org/remotecontent?filepath=org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar
我能夠使用javac * 從命令行 *編譯代碼,之后我從 jar中刪除了一些其他的(與eclipse相關的假設)條目,並且只保留了類和包。 清潔版的jar就在這里 。
我還刪除了通常由eclipse引入的@Override注釋。
你提到了錯誤的jar文件名 - “org.eclipse.osgi_3.8.0.v20120529-1548.jar”正確的文件名是org.eclipse.osgi-3.8.0.v20120529-1548.jar
此命令:javac.exe -cp org.eclipse.osgi-3.8.0.v20120529-1548.jar TestBundle.java
正在為我工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.