问题
I have a dropbox-core-sdk-1.7.5.jar archive file. I tried to install it as a package in my Home directory with:
java -jar dropbox-core-sdk-1.7.5.jar
but the terminal spits out: "no main manifest attribute, in dropbox-core-sdk-1.7.5.jar". The thread Can't execute jar- file: "no main manifest attribute" suggests that I need to add a line similar to "Main-Class: com.mypackage.MyClass" META-INF/MANIFEST.MF file. But I don't know what class I'm supposed to type in.
I've found instructions on http://www.wikihow.com/Run-a-.Jar-Java-File, but they are just as confusing.
Can someone explain to me what I should do about this non-executable jar file so that I could let the machine know that this package exists?
回答1:
Command Line:
If you are trying to build it without an IDE and are using the Main.java file included on the Dropbox Site...
// 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();
}
}
}
Use these command lines in a terminal from the appropriate directory:
To Compile: javac -cp dropbox-core-sdk-1.7.5.jar Main.java
To Run: java -cp .;dropbox-core-sdk-1.7.5.jar;jackson-core-2.2.3.jar Main
(use :
instead of ;
for Unix-like operating systems)
Eclipse IDE:
If you are using the Eclipse
IDE, you can simply copy the dropbox-core-sdk-1.7.5.jar
file into your project, right click on it, and select the menu option Build Path -> Add to Build Path
. It should then appear in your Referenced Libraries
folder in the Package Explorer
window, and then you should be able to import and use it.
Repeat the process for jackson-core-2.2.3.jar
.
This link has multiple step by step images of the general process of adding a .jar
file to your project if you prefer: 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
}
}
来源:https://stackoverflow.com/questions/20037741/cannot-install-a-jar-package-without-an-ide