【问题标题】:Cannot install a jar package without an IDE没有IDE无法安装jar包
【发布时间】:2013-11-17 23:45:38
【问题描述】:

我有一个 dropbox-core-sdk-1.7.5.jar 存档文件。我尝试将它作为一个包安装在我的主目录中:

java -jar dropbox-core-sdk-1.7.5.jar

但终端吐出:“没有主要清单属性,在 dropbox-core-sdk-1.7.5.jar 中”。线程Can't execute jar- file: "no main manifest attribute" 建议我需要添加类似于“Main-Class: com.mypackage.MyClass” META-INF/MANIFEST.MF 文件的行。但我不知道我应该输入什么类。

我在 http://www.wikihow.com/Run-a-.Jar-Java-File 上找到了说明,但它们同样令人困惑。

有人可以向我解释我应该如何处理这个不可执行的 jar 文件,以便我可以让机器知道这个包存在吗?

【问题讨论】:

  • 您不会“运行”那种 .jar 文件。您将其导入您的项目并使用它。您使用哪个 IDE 进行开发?如果您没有使用 IDE,这可能会对您有所帮助:stackoverflow.com/questions/4732892/…
  • @Ryan:javac -classpath dropbox-core-sdk-1.7.5.jar Main.java 工作。但是,java -classpath dropbox-core-sdk-1.7.5.jar Main 没有。
  • 是的,需要在您使用 javac 编译的每个文件中包含“.java”。
  • @Ryan : java -classpath dropbox-core-sdk-1.7.5.jar Main.java Error: Could not find or load main class Main.java
  • 尝试java -classpath . Main 或将. 替换为编译器输出目录所在的位置。

标签: java jar sdk package dropbox-api


【解决方案1】:

命令行:

如果您尝试在没有 IDE 的情况下构建它并使用 Dropbox Site 中包含的 Main.java 文件...

// Include the Dropbox SDK.
import com.dropbox.core.*;
import java.io.*;
import java.util.Locale;

public class Main
{
    public static void main(String[] args) throws IOException, DbxException
    {
        // Get your app key and secret from the Dropbox developers website.
        final String APP_KEY = "INSERT_APP_KEY";
        final String APP_SECRET = "INSERT_APP_SECRET";

        DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);

        DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",
            Locale.getDefault().toString());
        DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);

        // Have the user sign in and authorize your app.
        String authorizeUrl = webAuth.start();
        System.out.println("1. Go to: " + authorizeUrl);
        System.out.println("2. Click \"Allow\" (you might have to log in first)");
        System.out.println("3. Copy the authorization code.");
        String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

        // This will fail if the user enters an invalid authorization code.
        DbxAuthFinish authFinish = webAuth.finish(code);

        DbxClient client = new DbxClient(config, authFinish.accessToken);

        System.out.println("Linked account: " + client.getAccountInfo().displayName);

        File inputFile = new File("working-draft.txt");
        FileInputStream inputStream = new FileInputStream(inputFile);
        try
        {
            DbxEntry.File uploadedFile = client.uploadFile("/magnum-opus.txt",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            System.out.println("Uploaded: " + uploadedFile.toString());
        }
        finally
        {
            inputStream.close();
        }

        DbxEntry.WithChildren listing = client.getMetadataWithChildren("/");
        System.out.println("Files in the root path:");
        for (DbxEntry child : listing.children)
        {
            System.out.println("    " + child.name + ": " + child.toString());
        }

        FileOutputStream outputStream = new FileOutputStream("magnum-opus.txt");
        try
        {
            DbxEntry.File downloadedFile = client.getFile("/magnum-opus.txt", null,
                outputStream);
            System.out.println("Metadata: " + downloadedFile.toString());
        }
        finally
        {
            outputStream.close();
        }
    }
}

从适当的目录在终端中使用这些命令行:

编译:javac -cp dropbox-core-sdk-1.7.5.jar Main.java
运行:java -cp .;dropbox-core-sdk-1.7.5.jar;jackson-core-2.2.3.jar Main(对于类 Unix 操作系统,使用 : 而不是 ;

Eclipse IDE:

如果您使用Eclipse IDE,您只需将dropbox-core-sdk-1.7.5.jar 文件复制到您的项目中,右键单击它,然后选择菜单选项Build Path -> Add to Build Path。然后它应该会出现在Package Explorer 窗口中的Referenced Libraries 文件夹中,然后您应该能够导入和使用它。

jackson-core-2.2.3.jar重复该过程。

如果您愿意,此链接有多个分步图像,展示了将.jar 文件添加到您的项目的一般过程:http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

// Import whatever you want from the Dropbox SDK...
import com.dropbox.*; // everything
import com.dropbox.core.*;
import com.dropbox.core.http.*;
import com.dropbox.core.json.*;
import com.dropbox.core.util.*;

public class Test
{
    public static void main(String[] args)
    {
        // Do stuff
    }
}

【讨论】:

  • 你的代码不起作用,因为我得到了这个异常:线程“main”中的异常 com.dropbox.core.DbxException$BadRequest: {"error_description": "given \"code\" is not com.dropbox.core.DbxRequestUtil.unexpectedStatus(DbxRequestUtil.java:202) senthil 处的“有效”、“错误”:“invalid_grant”}
  • 正如我在答案顶部提到的,这段代码直接从 Dropbox 的网站复制而来(减去他们没有在一个字符串中转义引号的部分)。它编译得很好,但我不能评论它的功能,因为我没有亲自使用它。这只是他们为帮助您入门而提供的示例代码。虽然,如果我在看到该异常时进行任何猜测,我会说您输入的授权码无效。
猜你喜欢
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2018-09-28
相关资源
最近更新 更多