如何使用包编译和运行java类

时间:2015-07-26 12:00:42

标签: java package javac

我在文件MyClass.java文件中有一个名为MyClass的类(下面提到的代码)

package myclass;

class MyClass {
  public int add (int a, int b){
    return a+b;   
  }

  public static void main(String args[]) {
    MyClass obj = new MyClass();
    System.out.println(oobj.add(2, 3));
  }
}

我正在使用

编译该类
javac MyClass.java

但我正在尝试使用

运行该类
java MyClass

java myclass.MyClass

我收到了错误

Error: Could not find or load main class MyClass

但是,如果我省略了包名,我就能运行这个程序。 我哪里错了?

4 个答案:

答案 0 :(得分:6)

确保您位于包文件夹(编译的类文件所在的文件夹)的父目录中,并执行以下命令:

java myclass.MyClass

下面是一个示例文件结构:

bin
    -> myclass
        -> MyClass.class

在上面的示例结构中,您必须从“bin”目录执行命令。

此外,将类定义为public并重新编译java源文件。

答案 1 :(得分:2)

我也碰到了这个。对于其他语言的人来说,这是非常令人沮丧的。关键在于,java文件必须位于正确的目录中,具体取决于包声明。

如果java文件Test1.java以

开头
package com.xyz.tests;

然后java文件Test1.java需要在目录com/xyz/tests

您可以编译并以

运行
javac com/xyz/tests/Test1.java
java com/xyz/tests/Test1
祝你好运。

答案 2 :(得分:0)

您需要使用以下命令编译该类:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Chapman Automotive Skills Assessment</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<nav>
		<div class="brand">
		<img src="img/Logo.png" alt="logo" class="logo">
		<div class="comp-name">CHAPMAN</div>
		</div>

		<div class="nav-links">
			<div class="link">Home</div>
			<div class="link">Sales</div>
			<div class="link">Blog</div>
			<div class="link">Login</div>
		</div>
	</nav>
	<header>
		<div class="header-info">
			<p>We are a company that does stuff.</p>
			<p>Car and web stuff.</p>
		</div>
	</header>
	<main>
		<div class="col col-1">
			<img src="img/car1.jpg" alt="car1">

			<h3>Some Header</h3>

			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo tempore quia enim quod, perferendis illum quae id, natus dolores temporibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati, rem. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
		</div>

		<div class="col col-2">
			<img src="img/car2.jpg" alt="car2">

			<h3>More Stuff</h3>

			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo tempore quia enim quod, perferendis illum quae id, natus dolores temporibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo, dolor. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
		</div>

		<div class="col col-3">
			<img src="img/car3.jpg" alt="car3">

			<h3>Last Column</h3>

			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo tempore quia enim quod, perferendis illum quae id, natus dolores temporibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Esse, ipsa. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
		</div>
	</main>
	<footer id="footer">
		<div class="internal-links">
			<h4>Internal Links</h4>

			<div class="links">
				<div>Page One</div>
				<div>Another Page</div>
				<div>Sales Page</div>
				<div>Page Three</div>
				<div>Keep Going</div>
				<div>Last One</div>
				<div>Just Kidding</div>
			</div>
		</div>

		<div class="form-wrap">
			<form>
				<label for="Name">Name</label>
				<input type="text" class="name" required>
				<label for="Name">Address</label>
				<input type="text" class="address" required>
				<label for="Name">City</label>
				<input type="text" class="city" required>

				<button type="submit" id="submit">Submit Form</button>
			</form>

			<div class="dialog-wrap">
				<dialog>
					<div><span class="label">Name:</span><span class="dialog-name info"></span></div>
					<div><span class="label">Address:</span><span class="dialog-address info"></span></div>
					<div><span class="label">City:</span><span class="dialog-city info"></span></div>
					<div><button class="close">Close</button></div>
				</dialog>
			</div>
	</footer>
	<script src="script.js"></script>
</body>
</html>

答案 3 :(得分:0)

我的示例由

运行
java <package>.<class>

来自软件包的父目录