小编Blr*_*lrp的帖子

.jar错误 - 无法找到或加载主类

我试图将HelloWorld放在.jar文件中并运行它,但它不起作用.我创建了java文件并在程序中输入,然后在cmd中写入:

javac HelloWorld.java
java HelloWorld
Run Code Online (Sandbox Code Playgroud)

它起作用了.然后我进入了

echo Main-Class: HelloWorld >manifest.txt
jar cvfm HelloWorld.jar manifest.txt HelloWorld.class
Run Code Online (Sandbox Code Playgroud)

得到了输出

added manifest
adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)
Run Code Online (Sandbox Code Playgroud)

然后我进入了

java -jar HelloWorld.jar
HelloWorld.jar
Run Code Online (Sandbox Code Playgroud)

并且第一行有效,而第二行给了我一个错误:

Error: Could not find or load main class path\HelloWorld.jar
Run Code Online (Sandbox Code Playgroud)

当我尝试使用64位jre7\bin,jdk1.7.0_51\bin,jdk1.7.0_51\jre\bin中的java.exe文件打开它时,我获得的输出(在快速关闭的窗口中)是相同的输出以及32位jre7\bin.我已经卸载并重新安装了我的jre和jdk并重新创建了我的.class和.jar文件,但问题仍然存在.我在win8上.

编辑:我试图像aetheria那样建议,但没有运气.我把HelloWorld.java放在路径\ com\stackoverflow\user\blrp中,编译它,它通过输入工作

java com.stackoverflow.user.blrp.HelloWorld
Run Code Online (Sandbox Code Playgroud)

在路上.然后我创建了清单和jar:

(echo Manifest-Version: 1.0
echo Class-Path: .
echo Main-Class: com.stackoverflow.user.blrp.HelloWorld) >manifest.txt
jar cvfm HelloWorld.jar manifest.txt com\stackoverflow\user\blrp\HelloWorld.class
Run Code Online (Sandbox Code Playgroud)

得到了输出

added manifest
adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat
ed 31%)
Run Code Online (Sandbox Code Playgroud)

但仍然,java -jar HelloWorld.jar工作,而HelloWorld.jar没有(同样的错误).我也尝试过不做包装的东西,只是清单中的Class-Path,结果相同.

(另外,我在使用.bat文件提问之前解决了这个问题,但是让jar工作起来仍然很好.)

java jar

25
推荐指数
3
解决办法
10万
查看次数

在python 3中无空间打印

我是Python的新手,我想知道如何在不增加额外空间的情况下打印多个值.我想要输出ab而不是a b不必调用print两次:

print("a", end="")
print("b")
Run Code Online (Sandbox Code Playgroud)

另外,我有以下代码:

a = 42
b = 84 
Run Code Online (Sandbox Code Playgroud)

我想打印他们的价值观a = 42, b = 84,如果我这样做的话

print("a = ", a, ", ", b = ", b)
Run Code Online (Sandbox Code Playgroud)

添加额外的空格(输出a = 42 , b = 84)

而Java风格,

print("a = " + a + ", b = " + b)
Run Code Online (Sandbox Code Playgroud)

提出一个TypeError.

printing python-3.x

17
推荐指数
2
解决办法
2万
查看次数

Cloneable抛出CloneNotSupportedException

public class test implements Cloneable {
    @Override
    public test clone() {
        return (test) super.clone();
    }

    public static void main(String[] args) {
        new test().clone();
    }
}
Run Code Online (Sandbox Code Playgroud)

error: unreported exception CloneNotSupportedException当我尝试编译时(第4行,而不是主要部分),我得到了.据我所知,实现的整个目的Cloneable是摆脱异常.

  • 有没有一种方法可以使用super.clone()而不抛出或捕获异常?
  • 界面实际上做了什么吗?

java clone cloneable

4
推荐指数
2
解决办法
7765
查看次数

使用 UTF-16 将文件读取为字符串时,由 MalformedInputException 引起的“不应该发生”的错误

Path file = Paths.get("New Text Document.txt");
try {
    System.out.println(Files.readString(file, StandardCharsets.UTF_8));
    System.out.println(Files.readString(file, StandardCharsets.UTF_16));
} catch (Exception e) {
    System.out.println("yep it's an exception");
}
Run Code Online (Sandbox Code Playgroud)

可能会产生

some text
Exception in thread "main" java.lang.Error: java.nio.charset.MalformedInputException: Input length = 1
    at java.base/java.lang.String.decodeWithDecoder(String.java:1212)
    at java.base/java.lang.String.newStringNoRepl1(String.java:786)
    at java.base/java.lang.String.newStringNoRepl(String.java:738)
    at java.base/java.lang.System$2.newStringNoRepl(System.java:2390)
    at java.base/java.nio.file.Files.readString(Files.java:3369)
    at test.Test2.main(Test2.java:13)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
    at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
    at java.base/java.lang.String.decodeWithDecoder(String.java:1205)
    ... 5 more
Run Code Online (Sandbox Code Playgroud)

这个错误“不应该发生”。方法如下java.lang.String

private static int decodeWithDecoder(CharsetDecoder cd, char[] dst, byte[] src, int offset, int length) {
    ByteBuffer bb …
Run Code Online (Sandbox Code Playgroud)

java utf-16 java-17

4
推荐指数
1
解决办法
1891
查看次数

Java 扫描仪和字母 åäö

我想在 Java 中输入可能包含字母 åäö 的字符串,但 Scanner 将它们转换为其他字符。我也尝试过 utf-8:

String s1 = new Scanner(System.in).nextLine();
String s2 = new Scanner(System.in, "utf-8").nextLine();
System.out.println(s1 + "|" + (int)s1.charAt(0));
System.out.println(s2 + "|" + (int)s2.charAt(0));
System.out.println((int)'å' + "|" + (int)'?');
Run Code Online (Sandbox Code Playgroud)

这产生:

å
å
?|8224
?|65533
229|63
Run Code Online (Sandbox Code Playgroud)

所有字符都变成 65533 和 utf-8。如果没有 utf-8,ä 变为 8222,ö 变为 8221,Å 变为 65533,Ä 变为 381,Ö 变为 8482。

是否有一些替代输入法允许 åäö?

我正在运行 java 8u25 并且我正在从 Windows 控制台运行该程序。

java input

1
推荐指数
1
解决办法
3175
查看次数

java HashSet中的重复项

似乎HashSets中允许重复.为什么这样,我该如何去除它们,为什么第二个不能在remove()下面工作呢?删除所有重复项的一种方法是new HashSet<>(set),但有没有更好的方法不涉及创建新对象?

Set<ArrayList<String>> set = new HashSet<>();
ArrayList<String> a1 = new ArrayList<>();
ArrayList<String> a2 = new ArrayList<>();

a1.add("a");
set.add(a1);
a1.remove("a");

set.add(a2);

System.out.println(set.size());
System.out.println(set);

ArrayList<String> a3 = new ArrayList<>();
for (Object o : set) {
    boolean b = o.equals(a3) && (o.hashCode() == a3.hashCode());
    if (!b) System.out.println(false);
}

set.remove(new ArrayList<String>());
System.out.println(set);
set.remove(new ArrayList<String>());
System.out.println(set);
set.remove(set.iterator().next());
System.out.println(set);
System.out.println(set.iterator().next() == a1);
Run Code Online (Sandbox Code Playgroud)

输出:set由两个相等的空列表组成,最初不为空的列表无法删除.

2
[[], []]
[[]]
[[]]
[[]]
true
Run Code Online (Sandbox Code Playgroud)

java duplicates hashset duplicate-removal

1
推荐指数
1
解决办法
653
查看次数

为最后一个枚举值提供不同的变量值

我希望最后一个枚举在其中一个变量中具有不同的值:

private enum thing {
    thing0(0),
    thing1(1),
    thing2(2);

    int index;
    String s;

    private thing(int index) {
        this.index = index;
        s = index == values().length - 1 ? "b" : "a";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是行不通的;你不能values()在构造函数中调用。还有别的办法吗?

java enums

0
推荐指数
1
解决办法
69
查看次数

如何最好地使用 const 成员累积变量?

以下内容不起作用,因为Int有一个 const 成员,并且该=运算符被删除,因为它无法修改值:

struct Int {
    const int val = 1;
    Int(int val) : val(val) {}
    operator int() {
        return val;
    }
};

std::vector<Int> ints = {1, 2, 3, 4};
Int sum = 0;
for (Int i : ints) { sum = sum+i; }
std::cout << sum;
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用,也会出现同样的问题std::accumulate

我可以通过制作sum一个智能指针来解决这个问题,但它看起来很笨拙。有没有更好的办法?

c++

0
推荐指数
1
解决办法
166
查看次数