有多个文件时无法找到或加载主类

时间:2016-11-14 19:36:52

标签: java compilation javac

我有class A,B and Main A类使用B类,主要使用A类

我想用命令行运行这些文件 我做完了:

javac *.java
java Main

然后我得到Error: Could not find or load main class Main

代码: Location.java

package hw3;

public class Location {
    private int objectLength;
    private long byteLocation;

    public Location(int freshLength, long freshLocation)
    {
        objectLength = freshLength;
        byteLocation = freshLocation;
    }

    public int getLength()
    {
        return objectLength;
    }

    public long getLocation()
    {
        return byteLocation;
    }
}

FileMap.java

package hw3;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class FileMap<K, V extends Serializable> implements Map<K, V>{
    private HashMap<K, Location> database;  // Used to hold a key and the value location and length
    File fp;
    RandomAccessFile s;


    public FileMap(String filename) throws FileNotFoundException
    {
        database = new HashMap<K, Location>();
        fp = new File(filename);
        s = new RandomAccessFile(fp, "rws");
    }

    public void closeFile()
    {
        try {
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public byte[] serialize(V value) throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(value);
        return baos.toByteArray();
    }

    @SuppressWarnings("unchecked")
    public V deserialize(byte[] byteArray) throws IOException, ClassNotFoundException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (V)ois.readObject();
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean containsKey(Object key) {
        if(database.containsKey(key)) return true;
        return false;
    }

    @Override
    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Set<java.util.Map.Entry<K, V>> entrySet() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public V get(Object key) {
        if(database.containsKey(key))
        {
            try 
            {
                byte[] bar = new byte[database.get(key).getLength()];  // Create a byteArray long enough to hold the object
                s.seek((int)database.get(key).getLocation());  // Move file pointer to saved value location
                s.read(bar, 0, database.get(key).getLength());  // Read object
                try 
                {
                    return deserialize(bar);  // "Un-flatten" object and return it 

                } 
                catch (ClassNotFoundException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } 
            catch (IOException e) 
            {
                System.err.println("Couldn't read file");
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean isEmpty() {
        if(database.isEmpty()) return true;
        return false;
    }

    @Override
    public Set<K> keySet() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public V put(K key, V value) {
        if(!database.containsKey(key))
        {
            try 
            {
                byte[] ba = serialize(value);  // "flatten" object to byte array
                s.seek(s.length());  // Go to end of file
                s.write(ba);  // Write byte array to end of file
                database.put(key, new Location(ba.length,s.length()-ba.length));  // Save key in internal key-location map
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            System.out.println("Key already exists");
        }
        return null;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        // TODO Auto-generated method stub

    }

    @Override
    public V remove(Object key) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        return database.size();
    }

    @Override
    public Collection<V> values() {
        // TODO Auto-generated method stub
        return null;
    }
}

Main.java

package hw3;

import java.io.FileNotFoundException;

public class Main {

    public static void main(String[] args) {
        String fileName = "/home/rippxe/Documents/S2/Java/hw3/randFile.bin";
        Integer x1 = new Integer(152), x2 = new Integer(485), x3 = new Integer(825);
        String str1 = "bob",str2 = "john",str3 = "steve";
        FileMap<Integer, String> fm = null;
        try
        {
            fm = new FileMap<Integer, String>(fileName);

        }
        catch(FileNotFoundException fnfe)
        {
            System.err.println("File not found");
        }


        fm.put(x1, str1);
        fm.put(x2, str2);
        fm.put(x3, str3);

        String new1 = fm.get(x1);
        String new2 = fm.get(x2);
        String new3 = fm.get(x3);

        System.out.println(new1 + " " + x1 + "\n"+ new2 + " " +x2 +"\n"+ new3 + " " +x3);
        fm.closeFile();
    }

}

我已添加了我的课程代码,您可以在上面看到。 感谢

0 个答案:

没有答案